Introduction to Exceptions
1, System.Exception class
Message property: The cause of the exception and the contents of the exception
Souce property: Throws the name of an exception assembly
StackTrace Property: Method invocation case where an exception occurred
InnerException property: Exception contained in the secondary exception
2, try{}catch{}finally{}
Handling Exceptions
1. There is a difference between catch and catch (Exception) with no parameters
catch (Exception) captures all exceptions that derive from the Exception class, and catch with no parameters captures all exceptions, regardless of whether the exception derives from the Exception class.
2, catch with catch and finally is optional, but the two must choose one. A try can correspond to multiple catch, but a try can only correspond to a finally.
3. The statement in finally is bound to be executed regardless of whether an exception occurs in a try.
Abnormal propagation
If the exception occurs and is not caught by the corresponding catch, the exception is gradually passed up the call stack until an appropriate catch statement is encountered or is passed to the lowest calling method. If no catch is found, the exception is delivered to the. NET common language runtime, and the Common language runtime pops up a dialog box to display exception information.
Throw exception Throw
1, throw variable name;
Must be a exception exception or a exception-derived type
2, throw;
This throw statement has only one throw keyword and can only be used in a catch statement block, which means that the exception caught by the current catch statement is thrown.
Custom exceptions
Follow the principles
1, avoid the use of deep exception class inheritance hierarchy
2. Custom exception classes must inherit the System.Exception class or several other basic common exception classes
3, custom exception class name to end with exception
4. Custom exception classes should be serializable
5. The custom exception class should implement at least the following four constructors that are the same as the exception class
Public myexception () {}
Public myexception (String message) {}
Public myexception (String message,exception inner) {}
Protected MyException (System.Runtime.Serialization.SerializationInfo Info,system.runtim E.serialization.streamingcontext context) {}
6. Automatically insert custom exception class frame
You can get a custom exception class frame by typing exception in the editor and pressing the <Tab> key.