Difference between throw and throw ex

Source: Internet
Author: User

 

Previously, when I used the exception capture statement try... catch... throw, I didn't pay too much attention to the differences between several usage methods. I debugged it a few days ago.ProgramI accidentally learned that there are differences in the use of several methods. The online query is true, mainly because the difference lies in the starting point of stack information. Summary:

We all know that throw and throw ex are used in C # To throw an exception, but they are different.

Throw; is recommended in C # To throw an exception. Throw ex; clears all information so far and considers that the exception you caught has been handled, however, a new exception is thrown during the processing, and the real error source cannot be found.

Throw is mainly used in the following ways:

 

First (not recommended, but it is a pity that many people have always used this, including Yi and XI). This applies to the original exception points and resets the exception starting point in the stack:

View code

 
Try{}Catch(Exception ex ){ThrowEx ;}

 

Second, it can be traced back to the original exception point, but the compiler will warn that the defined ex is not used:

View code

 
Try{}Catch(Exception ex ){Throw;}

 

Third, without exception parameters, this is the same as the second one. All types of exceptions can be captured without the IDE warning:

View code

Try{}Catch{Throw;}

 

In fact, the second and third usage are not recommended in the book. Generally, we should start from small-granularity exception capture and adopt multiple catch statements...

 

Fourth, the original exception information is retained after the exception is repackaged. Recommended.

View code

Try{}Catch(Exception ex ){Throw NewException ("Further packaging exception", Ex );}

 

The following is an example:

View code

 1           ///   <Summary>  2           /// Entry Method  3           ///   </Summary>  4           Public   Static   Void  Main ()  5   {  6 Exceptionclass EC = New  Predictionclass ();  7   8              Try  9   {  10   EC. exceptionthrow1 ();  11   }  12               Catch  (Exception ex)  13   {  14   Console. writeline (ex. tostring ());  15   } 16   17 Console. writeline ( "  ---------------------------------------------------------------------  "  );  18   19               Try  20   {  21   EC. exceptionthrow2 ();  22   } 23               Catch  (Exception ex)  24   {  25   Console. writeline (ex. tostring ());  26   }  27   28 Console. writeline ( "  ---------------------------------------------------------------------  "  ); 29   30               Try  31   {  32   EC. exceptionthrow3 ();  33   }  34               Catch  (Exception ex)  35   {  36   Console. writeline (ex. tostring ()); 37   }  38   39 Console. writeline ( "  ---------------------------------------------------------------------  "  );  40   41               Try  42   {  43   EC. exceptionthrow4 (); 44   }  45               Catch  (Exception ex)  46   {  47   Console. writeline (ex. tostring ());  48   }  49   50 Console. writeline ( " ---------------------------------------------------------------------  "  );  51   52   Console. readkey ();  53   }  54   }  55   56       ///   <Summary>  57       /// This class is used to test the call of the context stack when an exception is thrown.  58       ///   </Summary>  59       Public   Class  Predictionclass  60   {  61           ///   <Summary>  62           ///  Throw an exception  63          ///   </Summary>  64           Public   Void  Predictionthrow1 ()  65   {  66               Try  67   {  68                   //  Call the original exception and throw a method to throw an exception.  69                   This . Exceptionmethod ();  70   }  71               Catch  (Exception ex)  72   {  73                   Throw  Ex;  74   }  75   }  76   77          ///   <Summary>  78           ///  Method 1 for throwing an exception  79           ///   </Summary>  80           Public   Void  Exceptionthrow2 ()  81   {  82               Try  83  {  84                   This  . Exceptionmethod ();  85   }  86               Catch  (Exception ex)  87   {  88                   Throw  ;  89   }  90  }  91   92           ///   <Summary>  93           ///  Method 2 for throwing an exception  94           ///   </Summary>  95           Public   Void  Predictionthrow3 ()  96   { 97               Try  98   {  99                   This  . Exceptionmethod ();  100   }  101               Catch  102   {  103                   Throw  ;  104  }  105   }  106   107           ///   <Summary>  108           ///  Method 3 for throwing an exception  109           ///   </Summary>  110           Public   Void  Predictionthrow4 () 111   {  112               Try  113   {  114                   This  . Exceptionmethod ();  115   }  116               Catch  (Exception ex)  117   {  118                  Throw   New Exception ( "  Further packaging exception  "  , Ex );  119   }  120   }  121   122           ///   <Summary>  123           ///  Original exception throw Method 124           ///   </Summary>  125           Private   Void  Predictionmethod ()  126   {  127               Throw   New  Dividebyzeroexception ();  128   }  129 }

 

The running result is as follows:

 

From the running result, we can see that the first method has eaten the original exception information. The other three methods can be traced back to the original exceptions. The fourth method is recommended. I hope you can understand these differences and enjoy coding...

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.