Exception Handling

Exception is a run time error. Exception Handling is a mechanism to handle run time errors.

Example

1. Suppose there are ten statements in your program as shown below:

Statement1;
Statement2;
Statement3;
Statement4;
Statement5;
Statement6;
Statement7;
Statement8;
Statement9;
Statement10;

2. While executing or running this program, you got an error at Statement5. As a result of error at Statement5, the program execution stops and all the remaining statements from 6 to 10 wont get executed.

3. If we perform exception handling at statement5, the program will continue execution and all the remaining statements i.e. 6 to 10 will get executed.

Generally we use try and catch blocks to handle the exceptions

  • Program statements that you want to monitor for exceptions are contained with in a try block
  • Code to handle the occurred exception will be contained in the catch block 

Before knowing how to handle the exception its very important to know what happens when we don't handle any exception. Lets find out what happens when we wont handle any exception in the next post.