C exception handling details
- 2020-05-19 05:41:43
- OfStack
Abnormal introduction
1, System. Exception class
Message attribute: the reason for the exception and the content of the exception
Souce property: the name of the assembly that throws the exception
StackTrace property: the method call case where an exception occurred
InnerException property: the exception contained in the secondary exception
finally 2, try catch {} {} {}
Handle exceptions
There is a difference between a, catch without parameters, and catch (Exception)
While catch (Exception) can catch all exceptions derived from the Exception class, catch with no arguments can catch all exceptions, whether or not the exception is derived from the Exception class.
b, catch and finally with catch are optional, but 2 must choose 1. One try can correspond to multiple catch, but one try can only correspond to one finally.
c, statement 1 in finally will be executed regardless of whether an exception occurs in try.
Exception propagation
If the exception is not caught by the corresponding catch after it occurs, the exception is passed up the call stack until the appropriate catch statement is encountered or passed to the lowest calling method. If the corresponding catch is not found, the exception is delivered. When net common language runs, a dialog box pops up to display the exception information.
Throws an exception throw
1. Variable name of throw;
Must be an Exception exception or have an Exception derived type
2, throw;
This throw statement has only one throw keyword and can only be used in the catch block, which means to throw an exception that is caught by the current catch statement.
Custom exception
Follow the principle of
1. Avoid deep exception class inheritance hierarchies
2. Custom exception classes must inherit from the System.Exception class or several other basic common exception classes
3. Custom exception class names should end with Exception
4. Custom exception classes should be serializable
5. The custom exception class should implement at least the same four constructors as the Exception class
public MyException (a) {}
public MyException ( string message ) {}
public MyException ( string message . Exception inner ) {}
protected MyException ( System.Runtime.Serialization.SerializationInfoinfo,System.Runtime.Serialization.StreamingContext context ) {}
6. Automatically insert custom exception class framework
Enter Exception in the editor and press the button to get the custom exception class framework.