2011-04-14 70 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); 
    } 
} 
相關問題