Example Illustrating Exception case 1

In this post I’ll discuss some Example illustrating Exception. In first case I will write a small code in which nothing will be done in order to handle exception.

lets look at the code and paste it to your IDE. Observe the output.

Now it is simple program. Here exception will occur at second statement in main method while dividing 12 by 0.

Observe output:

We can see that last statement is not executed after exception.

Conclusion from case 1:

  1. If exception arise in the program program terminates at that point.
  2. Rest of the code will be not executed after exception.
  3. When exception occurs default exception handler will show a message of above type.
  4. message contains name of thread, name of exception, message, location at which exception occurs.

Example Illustrating Exception case 2


Edit the above code and make like below. Surround the risky code in try and write exception handling code in catch block.


Here exception prone code is enclosed in try block. And catch block contain exception handling code i.e. code which is executed when exception occurs.

Analyze the output:

conclusions from case 2:

  1. try block contain exception prone code
  2. If exception arise inside try block, jvm executes code inside catch block
  3. Remaining part of the program execute after that.
  4. We can put custom code to execute at the time of exception in catch block.
  5. catch block is executed if and only if exception occur.

Now we have more cases we will look them in next post. Comment down for any query and discussion.


Facebook Comments