What kind of Exceptions cannot be handled? [duplicate]

扶醉桌前 提交于 2019-12-20 04:38:33

问题


Possible Duplicate:
List of exceptions that CAN'T be caught in .NET

As is documented, try/catch blocks can't handle StackOverflowException and OutOfMemoryException.

Are there any other Exceptions that also suffer from this limitation?


回答1:


Jeffrey Richter made several good points on this topic in his book CLR via C#, part "Trading Reliability for Productivity".

BTW, you can catch and handle OutOfMemmory:

For some reason that I can’t quite explain, this attention to detail is not done when writing code for the .NET Framework. Getting an out-of-memory situation is always possible and yet I almost never see any code containing a catch block to recover from an OutOfMemoryException. In fact, I’ve even had some developers tell me that the CLR doesn’t let a program catch an OutOfMemoryException. For the record, this is absolutely not true; you can catch this exception. In fact, there are many errors that are possible when executing managed code and I hardly ever see developers write code that attempts to recover from these potential failures.




回答2:


The only exception that cannot be caught directly is (a framework thrown) StackOverflowException. This makes sense, logically, as you don't have the space in the stack to handle the exception at that point. From the docs:

Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default.

ThreadAbortException can be caught, but will always get re-raised, so has unique behavior. From the docs:

ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block.

Reference: List of exceptions that CAN'T be caught in .NET



来源:https://stackoverflow.com/questions/12774975/what-kind-of-exceptions-cannot-be-handled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!