zoukankan      html  css  js  c++  java
  • 在多重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

     

    这找起错误来就方便多了。


  • 相关阅读:
    织梦后台如何安装
    Java JFrame实现无边框无标题
    SharePoint 2013的100个新功能之网站管理(一)
    cvFindContours之轮廓个数
    RequireJS 入门指南
    RESTFUL Service : based on Jersey
    hdu 1548 A strange lift(优先队列)
    SharePoint 用户配置文件服务
    SharePoint Foundation 搜索-PowerShell
    SharePoint 企业搜索-PowerShell
  • 原文地址:https://www.cnblogs.com/lichdr/p/98648.html
Copyright © 2011-2022 走看看