简体   繁体   English

如何在catch块c#中管理异常?

[英]How to manage exception in catch block c#?

I have following code. 我有以下代码。

try
{
    int s=10;
    int k=0;
    int stdsd = s / k;
}
catch (DivideByZeroException ext)
{
    FileStream fs = new FileStream(@"C:\temp\data.txt", FileMode.Open);  
    //encountered an exception as file doesn't exist.
}
catch (Exception ex)
{
}
finally
{
    //some code here.
}

In above code when exception occured then it will try to write that in one file in catch block.but when it trying to open that file that file doen't exists, so in such cases system crashed. 在上面的代码中,当发生异常时,它会尝试在catch块中的一个文件中写入它。但是当它试图打开该文件时该文件不存在,所以在这种情况下系统崩溃了。 I want to execute such critical code in finally block but due to exception in catch block it not going further to that line. 我想在finally块中执行这样的关键代码,但是由于catch块中的异常,它不会进一步到那一行。

I know we can check file exists check but I don't want to check file exist check over here, I want to how to manage that in catch block. 我知道我们可以检查文件存在检查但我不想检查文件存在检查这里,我想如何在catch块中管理它。 can please assist the best way to manage exception in catch block. 可以请帮助管理catch块中的异常的最佳方法。

You can't handle the exception generated from one catch block in a catch block associated with the same try/catch statement. 您无法处理与同一try/catch语句关联的catch块中的一个catch块生成的异常。 Instead, you need an extra one: 相反,你需要一个额外的:

catch (DivideByZeroException ext)
{
    try
    {
        FileStream fs = new FileStream(@"C:\temp\data.txt", FileMode.Open);  
    }
    catch (IOException e)
    {
        // Handle the exception here
    }
}

Note that you should be using a using statement when opening the stream, so that you'll close it automatically whether or not an exception is thrown later. 请注意,在打开流时应该使用using语句,这样无论是否稍后抛出异常,您都将自动关闭它。

I'd also recommend not having the code like this in the catch block directly - typically catch blocks should be short, for readability. 我还建议不要直接在catch块中使用这样的代码 - 为了便于阅读,通常catch块应该很短。 Consider moving all the error-handling functionality into a separate method (or possibly even a separate class). 考虑将所有错误处理功能移动到单独的方法中(或者甚至可能是单独的类)。

Just use another try {} catch {} ... 只需使用另一个try {} catch {} ......

try { 
    // ...
    try {
        // try opening your file here ...
    } catch (Exception) {
        // ...
    }
} catch (Exception) {
    // ...
}

If you don't want to check the existence of the file "over here" (in the exception), then you could alternatively check the existence "over there", that is: earlier in the code. 如果你不想检查文件是否存在“在这里”(在异常中),那么你可以选择检查“那边”的存在,即:代码中的早期。

What you want to do is to write an exception log to a file, as I understand it. 您想要做的是将异常日志写入文件,据我所知。 So you could configure the correct log file handling earlier in the code, in the best case with a good log file handler. 因此,您可以在代码中更早地配置正确的日志文件处理,最好使用良好的日志文件处理程序。 After this the only thing you have to do within the exception handler is to push your exception message to the log handler - which will then write this information properly to the file. 在此之后,您在异常处理程序中唯一需要做的就是将异常消息推送到日志处理程序 - 然后它将正确地将此信息写入文件。

exceptions are very powerful future of any language that can not be ignored in most cases , but try to avoid it if possible 异常是在大多数情况下不可忽视的任何语言的非常强大的未来,但如果可能的话尽量避免使用它

Look at this 看这个

            String File = "File.txt";
            System.IO.FileInfo fileinfo = new System.IO.FileInfo(File);

           if(fileinfo.Exists)
           {
               using (System.IO.FileStream file = new System.IO.FileStream(File, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite))
             {
                 // operation on file here
             }
           }else
           {
               using (System.IO.FileStream file = new System.IO.FileStream(File, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite))
               {
                   // operation on file here now the file is new file 
               }
           } 

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

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