Throw throws/passes an exception by using the throw statement in the Catch Block. exceptions that can be changed. For example, we can throw a new exception. Throw statements have various types and are necessary.
Example
First, let's take a look at the three methods called A, B, and C. They use different throw statements. Method A uses a throw statement without parameters. This can be seen as a rethrow (continue to throw)-It will throw the same exception that has already occurred
Continue. Method B throw is a named exception variable. This is not a complete rethrow-because it throws the same exception. But stacktrace is changed. If necessary, we can collect some exception information, and method C creates a new exception.
Tip: You can use this method to handle custom errors.
Example of using the throw statement
Using System; Class Program { Static Void Main (){ Try {A (); B (); C ( Null );} Catch (Exception ex) {console. writeline (Ex );}} Static Void A (){ // Rethrow syntax. Try { Int Value = 1 / Int . Parse ( " 0 " );} Catch { Throw ;}} Static Void B (){ // Filter exception types. Try { Int Value = 1 /Int . Parse ( " 0 " );} Catch (Dividebyzeroexception ex ){ Throw Ex ;}} Static Void C ( String Value ){ // Create a new exception. If (Value = Null ){ Throw New Argumentnullexception ( " Value " );}}}
ProgramPossible output results
System. dividebyzeroexception: attempted to divide by zero. system. dividebyzeroexception: attempted to divide by zero. system. argumentnullexception: value cannot beNull. Parameter Name: Value
Rethrow
Next, let's look at more details about rethrows. The rethrow must be a throw statement without parameters. If throw ex is used, targetsie (targetsite obtains the method for throwing this exception from the stack trace. If the stack trace is empty, targetsite also returns an empty reference. -Note by the translator) and stacktrace are both changed.
In the following program, the X () method uses the rethrow statement. Y () uses the throw ex statement. Let's take a look at the method that causes an exception when the rethrow statement is used, that is, the abnormal targetsite is within stringtonumber --- an int. parse method.
However, when Throw ex is used. Like in Y (), the abnormal targetsite is modified to the current y () method.
Example of testing rethrow
Using System; Class Program { Static Void Main (){ Try {X ();} Catch (Exception ex) {console. writeline (ex. targetsite );} Try {Y ();} Catch (Exception ex) {console. writeline (ex. targetsite );}} Static Void X (){ Try { Int . Parse ( " ? " );} Catch (Exception ){ Throw ; // [Rethrow construction] }} Static Void Y (){ Try { Int . Parse ( " ? " );} Catch (Exception ex ){ Throw Ex; // [Throw captured ex variable] }}}
Output
Void stringtonumber (system. String,...) void y ()
Summary:
The exception handling mechanism provides an optional control path that separates the exception logic from the exception handling. In addition, throw can be used to rethrow an exception or generate a new exception.
Translation: http://www.dotnetperls.com/throw
This article by bystander translation, reprint please note http://leaver.me