是否所有异常类型都被普通的旧"异常"所困扰?

soo*_*ise 4 c# exception

我只想更好地理解这一点.

我知道有许多不同的 异常类型,根据我所做的一些阅读,所有异常类型都被异常捕获.首先,我能确定这是真的吗?

try{
    ...
}
catch(Exception x){
    //No matter what fails in the try block, x 
    //will always have a value since all exception
    //types are caught under Exception? I guess
    //What I really want to know, is will this ever
    //Fail?
}
catch(SystemException x){
    //This code will never execute since all 
    //exceptions are caught in the first catch?
}
Run Code Online (Sandbox Code Playgroud)

接下来,这个捕获层次结构如何工作?如果Exception位于顶部,则每个其他异常在Exception下是一个级别,还是有多个类型层,例如Exception是ExceptionSomething的父级,它是ExceptionSomethingElse的父级?

附录:

或者,如果我们有这样的代码:

try{
    ...
}
catch(SystemException x){
    //If there is an exception that is not a SystemException
    //code in this block will not run, right (since this is 
    //looking specifically for SystemExceptions)?
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 8

它实际上取决于您的.NET版本和配置.在C++/CLI中你可以抛出任何东西 ; 它不一定是一个Exception.在1.1(IIRC)中,你只能通过以下方式捕获这些:

catch {...}
Run Code Online (Sandbox Code Playgroud)

这不是很有帮助 - 你看不到发生了什么.在2.0(IIRC)中,默认情况下,这些异常会自动包含在虚拟RuntimeWrappedException子类中.但是,为了兼容性,您可以将其关闭.求求你:不要:)