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...