1, 整體模式
PS:以上所有異常都在system命名空間裡,除了IOException及其派生Excpetion在system.IO內
可以看出異常主要分為2類,SystemException和ApplictaionException
SystemException:通常有.Net運行庫產生
ApplictaionException:是使用者定義異常的基類,可用於定義應用程式一些專屬的異常
2, SystemException
大部分都可以由其名字猜出作用
StackOverflowException:這樣的異常往往發生在遞迴以及死迴圈,造成分配給堆棧的記憶體地區已滿,這種情況下甚至不會執行finally地區。
OverflowException:比如checked環境下要把-40的int轉換為uint資料, 其基類即為計算異常基類。
其他的看單詞基本就能猜到了
3,ApplicationException
public class PageCannotFindException : ApplicationException{public PageCannotFindException(string pageName): base("Cannot find page: " + pageName){}public PageCannotFindException(string pageName, Exception innerException): base("Cannot find page: " + pageName, innerException){}}上面是一個簡單的使用者異常類
而是用的時候也是基本跟預定義的一樣
捕獲:
try{...}Catch(PageCannotFindException ex){...}
拋出:
Catch(Exception ex){throw new PageCannotFindException(pageName, ex);}