. NET exceptions (Exception)
The parent class of the exception in. NET is exception, and most exceptions generally inherit from exception.
You can customize the exception class by writing a class that inherits from Exception!
exception handling mechanism
Try
{
Code that might have occurred an exception
Subsequent code
}
Code outside of Try
catch (Exception e)
{
}
Finally
{
}
The above code is described below
1. who can perform in exception handling, once a try has a problem, the program discards the exception's subsequent code and jumps directly into the catch.
executes the code in the catch and resumes execution code other than the try .
2. about the parameter in catch () e
E is the exception class object that occurs, can be arbitrarily named. Not must be called E.
3. throw only one
The code in a try can only throw an exception.
Why is it?
Because once the exception is thrown, ah, there is no implementation of the back!
4. access to information
Exception information can be obtained by e.message
5. must perform
Finally it will be executed anyway
6. can not catch
Can be only try Catch
You can also have only try finally
excellent style of exception handling
1. Don't run away from the problem don't just catch the exception, do nothing, or just print it, this is not a good "exception handling" style.
Do not catch if you do not know how to handle the exception. Expose him to it. Since there is an exception, the problem is certain, escape is not a way to face him, to solve him. Especially in layered projects. Causes the program to fall into a state of deep logic chaos. And the problem is hidden, and you don't even know where it's going to go wrong.
2. What if you really encounter a random try, catch programmer?
VS is very powerful, and it's thinking of this. Click "Debug" "Exception" to enter such a tool, select the second row of the Raise option.
This way, when debugging, whether or not the try catch will burst out of the exception. The information we wanted was found.
. NET exceptions and exception handling