简体   繁体   English

引发异常时Catch Block的行为

[英]Behavior of Catch Block when exception is raised

Is there any case where catch block is skipped when an Exception is raised say 是否有任何情况下,当引发异常时跳过catch块

try 
{
  some code
}
catch(Exception ex)
{
  some code
}

I am using Exception class as it catches all the Exceptions. 我正在使用Exception类,因为它捕获所有异常。

You might have problems with StackOverflowException (see List of exceptions that CAN'T be caught in .NET ) 您可能遇到StackOverflowException问题(请参阅.NET中无法捕获的异常列表

There is another family of cases, specifically when the thing thrown wasn't an Exception , but this is only if you are either in 1.1, or don't have automatic exception wrapping enabled (it is enabled by default from 2.0) - it is theoretically possible for C++ to throw anything (not just an Exception ), so if your "some code" calls into some C++ that throws, say, a string , then in theory you could miss it. 还有另一类案例,特别是当抛出的东西不是Exception ,但这只是你在1.1中,或者没有启用自动异常包装(默认情况下从2.0启用) - 它是从理论上讲,C ++可以抛出任何东西 (不仅仅是一个Exception ),所以如果你的“某些代码”调用一些C ++抛出一个string ,那么理论上你可能会错过它。

In reality this is rarely (if ever) a real problem: 实际上,这很少(如果有的话)是一个真正的问题:

  • the automatic wrapping is enabled by default 默认情况下启用自动换行
  • most C++ code talking to .NET throws Exception (or a subclass) to be well-behaved 与.NET交谈的大多数C ++代码都会抛出Exception (或子类)以表现良好

In such cases, a catch { ... } will work to intercept that something was thrown, but won't tell you what happened. 在这种情况下,一个catch { ... }将拦截某些东西被抛出,但不会告诉你发生了什么

All exceptions have Exception as base type. 所有异常都将Exception作为基本类型。 If the code in the try block causes an exception, it will cause the code in the catch block of matching type to be executed. 如果try块中的代码导致异常,则会导致匹配类型的catch块中的代码被执行。 If you also have a more specific catch block that matches, that block will be executed: 如果您还有一个匹配的更具体的catch块,那么该块将被执行:

try
{
    throw new FileNotFoundException();
}
catch (FileNotFoundException)
{
    // this code will run
}
catch (Exception)
{
    // this won't (but will if a different exception is thrown)
}

If the code in the catch block causes an Exception , it will leave the catch block and find a "parent" catch block that matches. 如果catch块中的代码导致Exception ,它将离开catch块并找到​​匹配的“父” catch块。

Note that only exceptions on the current thread are caught - if you start and wait for asynchronous operations within the try block, you may not catch them on the waiting thread. 请注意,只捕获当前线程上的异常 - 如果您在try块中启动并等待异步操作,则可能无法在等待的线程上捕获它们。

As Marc has pointed out there are few exceptions that cannot be caught - for example, StackOverflowException. 正如Marc所指出的那样,很少有异常无法捕获 - 例如,StackOverflowException。

With .NET 4, there is concept of Corrupted State Exceptions to indicate exceptions that are not delivered to managed code unless code has explicitly indicated an interest in handling them. 对于.NET 4,存在损坏状态异常的概念,以指示未传递给托管代码的异常,除非代码明确表示有兴趣处理它们。 These exceptions (eg AccessVoilationException) indicates serious programming error and in general, does not guarantee the consistent memory/program state when occurred. 这些异常(例如AccessVoilationException)表示严重的编程错误,并且通常不保证发生时的内存/程序状态一致。 See this excellent article for more information: http://msdn.microsoft.com/en-us/magazine/dd419661.aspx 有关更多信息,请参阅此优秀文章: http//msdn.microsoft.com/en-us/magazine/dd419661.aspx

If there is an exception with the CLR (such as stack overflow) - it will not catch that Exception. 如果CLR存在异常(例如堆栈溢出) - 它将不会捕获该异常。

But for normal exceptions, you'll catch everything. 但是对于正常的例外情况,你会抓住一切。

It shouldn't skip as all exceptions are derived from Exception class. 它不应该跳过,因为所有异常都是从Exception类派生的。 Did you have any kind of occasion where it happened the other way? 你有没有其他方式发生过这种情况?

try  
{   
    //some code 
} 
catch(System.DivideByZeroException dbz)
{
    //Divide by Zero exception catched
}
catch(Exception ex) 
{   
    //some code 
} 

You have given Exception class which is the base class for all exception. 您已经给出了Exception类,它是所有异常的基类。 So you need to give specific Exception class like ArgumentNotFound Exception. 因此,您需要提供特定的Exception类,如ArgumentNotFound Exception。

If you dont want to catch any Exception just leave it blank. 如果您不想捕获任何异常,请将其留空。 But keep one thing in mind, using Try..Catch Block use high resource, so it is advisable to use it only if you are sure what kind of exception may occur. 但请记住一件事,使用Try..Catch Block使用高资源,因此建议仅在您确定可能发生何种异常时才使用它。

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

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