日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在多重Catch的情况下得到异常的完整信息

發布時間:2025/3/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在多重Catch的情况下得到异常的完整信息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在方法多層調用的時候,每一層都有相應的catch處理后重新throw的情況下,剛開始在最外層無法得到產生異常的完整信息。

最初的代碼樣子如下:


static?void?Main(string[]?args)

?????????{

??????????????try{

???????????????????Method3();

??????????????}catch(Exception?e)?{

???????????????????Console.WriteLine(e.ToString());

??????????????}

?

??????????????Console.Read();

?????????}

?

?????????static?void?Method1(int?a,int?b)?{

??????????????try{

???????????????????int?c?=?a?/?b;

??????????????}catch(DivideByZeroException){

???????????????????throw?new?CustomException("Parameter?b?can't?be?0");

??????????????}

?????????}

?

?????????static?void?Method2()?{

??????????????try{

???????????????????。。。

???????????????????Method1(1,0);

??????????????}catch(CustomException?e){

???????????????????。。。

???????????????????throw?e;

??????????????}

?????????}

?

?????????static?void?Method3()?{

??????????????try{

???????????????????Method2();

???????????????????。。。

??????????????}catch(CustomException?e)?{

???????????????????。。。

???????????????????throw?e;

??????????????}

?????}


(這只是示例,實際調用的時候當然不會太有同一個類里調用這么深的,有可能會在好幾個類之間調用。CustomException是一個自定義的異常)。

輸出:

CustomException.CustomException:?Parameter?b?can't?be?0

???at?CustomException.Class1.Method3()?in?。。。/class1.cs:line?58

???at?CustomException.Class1.Main(String[]?args)?in?。。。/class1.cs:line?19

?

在Method1里出的異常,可顯示出來的信息只到了Method3。在代碼錯綜復雜的情況,出的這種信息有點讓人不知所措。

?

看《.NET框架程序設計(修改版)》一書得知,原來小改一下就好多了。把所有throw?e;的地方改為throw;就可以得到下面的輸出:

CustomException.CustomException:?Parameter?b?can't?be?0

???at?CustomException.Class1.Method1(Int32?a,?Int32?b)?in。。。/class1.cs:line?41

???at?CustomException.Class1.Method2()?in?。。。/class1.cs:line?50

???at?CustomException.Class1.Method3()?in。。。/class1.cs:line?58

???at?CustomException.Class1.Main(String[]?args)?in?。。。/class1.cs:line?19

原來在throw?e;這樣寫的時候,CLR會重新設置異常的起始點,所以就把之前的信息給丟失了。

?

這明顯比第一次好多了。但很多時候,我們不會把異常經相應的處理后原樣拋出,有時會轉化為另一個異常后再拋出,這種情況下又是與剛開始時一樣的,會丟失信息。這種丟失雖說是預期的,本就是要對外表現為那個樣子的,但在自己開發調試的時候這種情況很煩,總是無法第一時間找到異常發生的比較確切的地方,只是縮小一下范圍而已。

這個時候就要用那個帶InnerException參數的構造器了(書上說在FCL中很多異常類型沒有提供相應的構造器,但我碰到幾個怎么都是有的;而且我們現在多是一些自定義異常為多),然后用ToString方法得到完整的信息。

最后:

?????????static?void?Main(string[]?args)

?????????{

??????????????try{

???????????????????Method3();

??????????????}catch(Exception?e)?{

???????????????????Console.WriteLine(e.ToString());

??????????????}

?

??????????????Console.Read();

?????????}

?

?????????static?void?Method1(int?a,int?b)?{

??????????????try{

???????????????????int?c?=?a?/?b;

??????????????}catch(DivideByZeroException?e){

???????????????????throw?new?CustomException("Parameter?b?can't?be?0",e);

??????????????}

?????????}

?

?????????static?void?Method2()?{

??????????????try{

???????????????????。。。

???????????????????Method1(1,0);

??????????????}catch(CustomException){

???????????????????throw;

??????????????}

?????????}

?

?????????static?void?Method3()?{

??????????????try{

???????????????????Method2();

???????????????????。。。

??????????????}catch(Exception?e)?{

???????????????????throw?new?CustomException("Invoke?Method3?Error",e);

??????????????}

?????}

得到:

CustomException.CustomException:?Invoke?Method3?Error?--->?CustomException.Custo

mException:?Parameter?b?can't?be?0?--->?System.DivideByZeroException:?試圖除以零



???at?CustomException.Class1.Method1(Int32?a,?Int32?b)?in。。。/class1.cs:line?36

???---?內部異常堆棧跟蹤的結尾?---

???at?CustomException.Class1.Method1(Int32?a,?Int32?b)?in。。。/class1.cs:line?44

???at?CustomException.Class1.Method2()?in。。。/class1.cs:line?52

???at?CustomException.Class1.Method3()?in。。。/class1.cs:line?58

???---?內部異常堆棧跟蹤的結尾?---

???at?CustomException.Class1.Method3()?in。。。/class1.cs:line?60

???at?CustomException.Class1.Main(String[]?args)?in。。。/class1.cs:line?19

?

這找起錯誤來就方便多了。

總結

以上是生活随笔為你收集整理的在多重Catch的情况下得到异常的完整信息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。