An exception is an issue that occurs during program execution. Exceptions in C # are a response to special situations that occur when a program runs, such as trying to divide by 0.
Exception handling statements provide a way to transfer control of a program from one part to another. C # exception handling is built on four keywords:try,catch,finally , and throw.
First, try and catch
The try and catch are used to detect exceptions in the program, and if there is no exception the program will run normally, and if an exception occurs, it will immediately stop executing the try statement and jump to the catch statement execution.
Console.Write ("Please enter a number");
String a = Console.ReadLine ();
Try
{
int b =convert.toint32 (a);
}
Catch
{
Console.WriteLine ("Input is not a number!") ");
}
Second, finally
When try and Cathy Run, the finally statement is run,
But it runs without the finally statement, so the finally statement is quite useless.
Console.Write (" Please enter a number "= console.readline (); Try { int b =Convert.ToInt32 (a);} Catch {Console.WriteLine (" input is not a number! ");}
Finally
{
Console.writr ("The last sentence");
}
Here is the finally dispensable, because the inside of the statement will always be executed.
Iv. Exception Handling Statement infrastructure
Try
{
Statements that may appear to be abnormal;
If the above statement has an exception, this statement does not execute, jump directly to catch;
}
Catch
{
Statements executed after an exception occurred;
}
Finally//can write not write
{
Whether there is any exception above will be executed at the end of the statement;
}
C # Exception Handling statements