Exception Handling
Exception Handling is a feature that is used to avoid or control the runtime errors generated by the program.
There are the following 3 types of errors.
- Syntax Errors
Syntax errors are also called compilation errors. These errors will occur when you compile the program and if there is any error in the syntax it will show the error, so if you have not written the program properly.
Example: Missing Semicolon at the end of a line.
a) You do something wrong and it shows you an error called a compilation error.
b) If you know the subject properly the moment that error occurs you will know what is the mistake and you will rectify it.
- Logical Errors
These are the ones where there is no error in the program. Syntactically everything is written properly but that the error is there in the logic, that is very difficult to identify because when you compile it will not show the error, when you run the program it will not show you the error but still error is there.
Where is the error?
Output
The result will not be correct/accurate. There are logical errors that are very difficult to trace.
Example:
I want to do 5+3, instead of 5+3 by mistake I have written 5-3.
Will it show an error?
No
Example:
If you say 5+3 it will give the answer 8.
If you say 5-3 it will give the answer 2.
So it's up to you to recognize whether the answer is correct or not you should cross-check then you know whether the answer is correct/not.
- Runtime Errors
Syntactically it is written properly but when you run the program you will get an error, why you will get the error at runtime when you have written it properly, these errors occur when you try to do some impossible tasks with the computer, that is not possible for your system to do.
Example
5/0--> infinity
- Exception handling doesn't provide a solution to the error.
- Whenever a runtime error occurs it avoids the runtime error on the screen and it provides a systematic end to the program.
- Exceptional handling should not be used for every runtime error that occurs in the program.
Note: It should be used as a LastChoice when it is not possible for a programmer to avoid the error by writing any logic.
Exception Handling consists of 4 Blocks:
- try
- catch
- finally
- throw
Try block:
- A Try block detects runtime errors.
- Whenever a runtime error occurs within the try block, it detects the error and sends the error information to the catch block.
- A Catch block receives the error information and provides a systematic end to the program.
Note: If any error occurs outside the try block then a runtime error appears on the screen.
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- int x, y, z;
- x = 5;
- y = 0;
- z = x / y;
- MessageBox.Show("result is" + z.ToString());
- }
- catch (Exception ex)
- {
- MessageBox.Show("cannot divide by zero");
- MessageBox.Show(ex.Message);
- MessageBox.Show(ex.StackTrace);
- MessageBox.Show(ex.ToString());
- }
- }
Message
Message is a property that returns the error message generated by a program.
StackTrace:
It is a property that returns the details of error.
ToString():
It is a function that returns the complete information of the error.
If there is a possibility of various types of errors then multiple catch blocks are used, if different customized messages are required.
Note: A Catch block will work only when there is a runtime error.
Finally
The following describes a Finally block:
- It is executed irrespective behavior of the program.
- If there is runtime error in the program, the Finally block is executed after the catch block.
- If there is no runtime error in the program, the Finally block is executed after the try block.
This is generally used for completing/finishing, if anything is pending in your application that has not been completed in the previous block then you can finish it in the finally block.For example: Normally used with database connections and filehandling connections to close the connections that could not be close in earlier part.
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- int x,y,z;
- x=5;
- y=0;
- z=x/y;
- MessageBox.Show("result is"+z.ToString());
- }
- catch(DivideByZeroException ex)
- {
- MessageBox.Show("cannot divide by zero");
- }
- catch(NullReferenceException ex)
- {
- MessageBox.Show("cannot process null");
- }
- catch(Exception ex)
- {
- MessageBox.Show("unable to execute");
- }
- finally
- {
- MessageBox.Show("finally block executed");
- }
- }
ThrowA throw is used to generated the errors manually or programmatically rather than waiting from the program to generate the errors.
- private void button3_Click(object sender, EventArgs e)
- {
- try
- {
- int x, y, z;
- x = 5;
- y = 0;
- if (y == 0)
- {
- DivideByZeroException d = new DivideByZeroException("cannot divide");
- throw d;
- }
- z = x / y;
- MessageBox.Show("result is" + z.ToString());
- }
- catch (DivideByZeroException ex)
- {
- MessageBox.Show(ex.Message);
- }
- }