在多重Catch的情况下得到异常的完整信息
生活随笔
收集整理的這篇文章主要介紹了
在多重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
?
這找起錯誤來就方便多了。
最初的代碼樣子如下:
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的情况下得到异常的完整信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python瀑布图怎么做_教你用Pyth
- 下一篇: WMI 的一个实现