如果在Catch块本身中发生异常,那么如何在C#中处理它? [英] If exception occurs in Catch block itself then how to handle it in C#?

查看:62
本文介绍了如果在Catch块本身中发生异常,那么如何在C#中处理它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//我已经在Catch Block中编写了代码

//I have written code in Catch Block

 try {

 } catch(Excepetion ex) {
        // I have written code here If Exception Occurs then how to handle it.
 }

推荐答案

您可以将try catch放入catch块中,也可以简单地再次引发异常.最好最后阻止您可以尝试catch,以便即使catch块中发生异常,也将最终执行块代码.

You can put a try catch inside the catch block, or you can simply throw the exception again. Its better to have finally block with your try catch so that even if an exception occurs in the catch block, finally block code gets executed.

try
  {
  }
catch(Excepetion ex)
  {
     try
        {
        }
     catch
        {
        }
   //or simply throw;
  }
finally
{
  // some other mandatory task
}

在某些例外情况下,可能无法最终执行阻止.您可能会看到约束执行区域,以获得更可靠的机制.

Finally block may not get executed in certain exceptions. You may see Constrained Execution Regions for more reliable mechanism.

这篇关于如果在Catch块本身中发生异常,那么如何在C#中处理它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆