2011-04-14 67 views
4

我只是想更好地理解这一点。是否所有的异常类型都在普通的旧“异常”之下?

我知道有很多不同exception types,根据我所做的一些阅读,所有异常类型都被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? 
} 

接下来,该捕获层次结构如何工作?如果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)? 
} 

回答

8

它实际上取决于你的.NET版本和组态。在C++/CLI中,你可以扔任何东西;它不一定是一个Exception。在1.1(IIRC)OU只能用像catch块捕捉这些:

catch {...} 

这是不是很有益的 - 你不能看到发生了什么。在2.0(IIRC)默认这种例外自动包装在一个虚拟RuntimeWrappedException子类。但是,为了兼容性,您可以关闭此功能。我求求你:不要:)

+0

如果我不添加任何引发任何疯狂的代码,只捕获异常应该足够好吗? – sooprise 2011-04-14 18:37:17

+0

@sooprise是;在任何理智的代码中,捕获异常是很好的;但只做记录等等。并且记得“抛出”,而不是“抛出”重掷。 – 2011-04-14 18:39:40

4

是的,这工作,因为所有标准例外从Exception继承。

您的代码可以正常工作,但您需要将Exception的处理程序全部设置为专用的异常类型。 (将执行第一个匹配处理程序。)

不会从Exception继承的异常不会被捕获,因为它们不是指定的类型。但是,.NET异常类都是从这个基类继承而来的。

有些人不认为这是一个好主意,但我通常只捕获Exception,除非我想特殊处理特定的异常类型。

1

是的,异常继承自Object类,并且all exceptions继承自Exception类。

上面的链接会显示所有异常的层次结构。

System.Object 
    System.Exception 
    Microsoft.Build.BuildEngine.InternalLoggerException 
    Microsoft.Build.BuildEngine.InvalidProjectFileException 
    Microsoft.Build.BuildEngine.InvalidToolsetDefinitionException 
    Microsoft.Build.BuildEngine.RemoteErrorException 
    ... 

一些例外,如SystemException的你提到从他们继承了进一步的例外情况,但他们都还是从Exception类继承:

System.Object 
    System.Exception 
    System.SystemException 
     Microsoft.SqlServer.Server.InvalidUdtException 
     System.AccessViolationException 
     System.Activities.ValidationException 
     System.AppDomainUnloadedException 
     System.ArgumentException 
     System.ArithmeticException 
     ... 
1

后者,例外可以继承基类Exception类或任何其他继承基类的类。

例如:从DbExceptionSqlException继承从ExternalExceptionSystemException其最终从Exception继承继承继承。

1

是的,所有异常类型都是从异常继承的。

而且,继承的工作方式,你可以有继承

MyAppException : Exception { 
} 

MyAppFileNotFoundException : MyAppException { 
} 

的多个水平。这通常是用来与不同类型的异常

try { 
    openfile('thisFileDoesNotExist.txt'); 
} 
catch (MyAppFileNotFoundException ex) 
{ 
     //warn the user the file does not exist 
} 
catch (Exception ex) 
{ 
     //warn the user an unknown error occurred, log the error, etc 
} 
2

的不同的行为要回答的第二部分您的问题,从。NET Framework的例外层次结构的示例:

ArgumentNullException inherits from 
ArgumentException inherits from 
SystemException inherits from 
Exception  

您应该尝试处理您可以的最具体的情况。

try{ 
    //something 
} 
catch(ArgumentNullException ex){ 
    //handle ArgumentNullException 
} 
catch(SystemException ex1) 
{ 
    //handle other kinds of SystemException 
    if(IDontWantToHandleThisExceptionHere(ex1)) 
    { 
     throw;// not throw new Exception(ex1); 
    } 
} 
相关问题