简体   繁体   English

我如何捕获异常块内引发的异常

[英]How i can catch an exception raised inside an Exception block

I am working on a .net console application, and i have the following code:- 我正在.net控制台应用程序上工作,并且我有以下代码:-

try {
    SPFile destFile = projectid.RootFolder.Files.Add(destUrl, fileBytes, false);
} catch (System.IO.DirectoryNotFoundException e) {
    SPFile destFile = projectid.RootFolder.Files.Add(destUrl, fileBytes, false);
} catch {}

now if an exception is raised inside the try block, the 2 other catch blocks will catch it, depending on the exception type!!. 现在,如果在try块内引发了异常,则其他2个catch块将捕获该异常,具体取决于异常类型! but if an exception is raised inside the catch (System.IO.DirectoryNotFoundException e) block then the console will end. 但是如果catch (System.IO.DirectoryNotFoundException e)块内引发了异常,则控制台将结束。 now i thought if an exception is raised inside the catch (System.IO.DirectoryNotFoundException e) block, then the last catch block will be reached but seems this is not the case.. so can anyone advice how i can catch an exception raised inside an Exception block?? 现在我以为如果在catch (System.IO.DirectoryNotFoundException e)块内引发了异常,则将到达最后一个catch块,但似乎并非如此。.所以有人可以建议我如何捕获在内部引发的异常异常块?

You should notice that try-catch should never be part of your code logic which means you should never use try-catch to control your branch. 您应该注意, try-catch绝不能成为代码逻辑的一部分,这意味着您绝不应该使用try-catch来控制分支。 This is the reason why you'll find it hard to let the exceptions flow through your each catch block. 这就是为什么您很难让异常流过每个catch块的原因。

If you'd like to catch the second block, you can write like this (but not recommended ): 如果您想抓住第二个区块,可以这样编写(但不推荐使用 ):

try
{
    SPFile destFile = projectid.RootFolder.Files.Add(destUrl, fileBytes, false);
}
catch (System.IO.DirectoryNotFoundException e)
{
    try
    {
        SPFile destFile = projectid.RootFolder.Files.Add(destUrl2, fileBytes, false);
    }
    catch
    {
        // Do what you want to do.
    }
}
catch
{
}

You'd better not to write like this above. 您最好不要这样写。 Instead, it's recommended to detect the folder exists ahead like this: 相反, 建议像下面这样检测文件夹是否存在:

try
{
    YourMainMethod();
}
catch (Exception ex)
{
    // Handle common exceptions that you don't know when you write these codes.
}

void YourMainMethod()
{
    var directory = Path.GetDirectoryName(destUrl);
    var directory2 = Path.GetDirectoryName(destUrl2);

    if (Directory.Exists(directory))
    {
        SPFile destFile = projectid.RootFolder.Files.Add(destUrl, fileBytes, false);
    }
    else if (Directory.Exists(directory2))
    {
        SPFile destFile = projectid.RootFolder.Files.Add(destUrl2, fileBytes, false);
    }
    else
    {
        // Handle the expected situations.
    }
}

要解决此问题,您必须在正在处理DirectoryNotFoundException的catch块中编写另一个try .. catchtry .. catch

It might make more sense to use File.Exists to see if the path already exists, and then attempt to write the file: 使用File.Exists可能更有意义,以查看路径是否已经存在,然后尝试写入文件:

string path = null;

if(!File.Exists(destUrl))
{
    path = destUrl;
}
else
{
    if(!File.Exists(destUrl2))
    {
        path = destUrl2;
    }
}

if(!string.IsNullOrWhiteSpace(path))
{
    try
    {
        SPFile destFile = projectid.RootFolder.Files.Add(path, fileBytes, false);
    }
    catch
    {
        // Something prevented file from being written -> handle this as your workflow dictates
    }
}

Then, the only exception you would expect to happen is a failure to write the file, which you would need to handle as your application dictates (permission issues should be treated differently than invalid binary data, corrupted streams, etc.) 然后,您唯一希望发生的异常是写入文件失败,您将需要根据应用程序的指示进行处理(权限问题应与无效的二进制数据,损坏的流等不同地对待)

You might find this worth a read, if you haven't done so: Best practices for exceptions 如果您还没有这样做,那么您可能会觉得值得一读: 例外的最佳实践

Depending on your needs, you could be try-catching in huge nested statements. 根据您的需求,您可以尝试捕获巨大的嵌套语句。

If you have a list of destinations you want to try, then you could do something like this 如果您有要尝试的目的地列表,则可以执行以下操作

var destinations = new List<string>() {dest1,dest2,dest3, ...};

SPFile destFile = null;

foreach(var destination in destinations)
{
    try
    {
        destFile = projectid.RootFolder.Files.Add(destination, fileBytes, false);
        // we are working
        Console.WriteLine($"Destination Success!!: {destination}");
        break;
    }
    catch(DirectoryNotFoundException ex)
    {
         Console.WriteLine($"Destination failed : {destination} - {ex.Message}");
    }
} 

if(destFile != null)
   // do something with your destFile 
else
   // oh noez!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM