In the software development process, the program exception is very common, that is, we often say the bug, so we need to deal with unpredictable exceptions.
Exception handling is actually very simple, at first I also have to be difficult, do not fear, really very simple, really understand the most also on a try-catch statement and throw statement.
Try-catch Statement Usage:
try{
Statement block 1//code that could throw an exception
}
catch (Exception type 1 exception object 1) {
Statement Block 2//exception handling
}
catch (Exception type 2 exception object 2) {
Statement block 3//exception handling
}
When an exception occurs in a try statement block, the program first creates an exception object and then searches for the matching catch object, and if a matching block of code is found, the program jumps to the catch block and executes the statement in the block.
Here's an example of exception handling for factorial:
1, first give everyone did not do exception handling code:
I will not cut, when we enter in the input box is not a number, the system will produce an exception, the program is interrupted. This is the most common input exception.
2, the following is a Try-catch statement to the possible exception of the statement written in a try statement block, in the catch statement block processing:
This is when we do not enter a number in the input box will pop up a prompt box said that I format input is not correct, this is the exception processing.
C #. NET Programming Foundation-try-catch exception Handling-factorial example