Problem without exception handling in java

exception handling

Problem without exception handling




Following is the simple program with divide by zero situation(ArithmeticException-click here to know how Arithmetic Exception occurs)We didnt apply exception handling technique.
  1. class Simple{
  2. public static void main(String args[]){
  3. int data=50/0;
  4. System.out.println("some code");
  5. }
  6. }
 Output:
Exception in thread main java.lang.ArithmeticException:/ by zero
As observed in the output,The statement "some code" is not printed.
In the above program ArithmeticException exception is occurred and the exception is
not handled hence it is handled by jvm.In this situation JVM will terminate the program and prints the cause for termination.
Note:
The following lines after the exception are not printed if the exception is not handled properly.

Solution by exception handling:




Below is the solution,In this program we have applied try and catch.
We have enclosed the doubtful code which might raise the exception in try block and catch is the handler.The statements in catch block will execute when there is an arithmetic exception in the try block.After executing try block ,the statements after catch block will also execute as the exception is handled i.e "rest of the code" statement is also executed. The following is the program with exception handling
  1. class Simple{
  2. public static void main(String args[]){
  3. try{
  4. int data=50/0;
  5. }
  6. catch(ArithmeticException e)
  7. {
  8. System.out.println(e);
  9. }
  10. System.out.println("some code");
  11. }
  12. }

Can we write Multiple catch blocks for a try?

Yes,We can write multiple catch blocks for a try block.
When we are not sure about which exception may be raised in the try block then we can writemultiple catch blocks 
to handle the exception.
Below is the sample program with multiple catch blocks:
  1. class MultipleCatch {
  2. public static void main(String args[]) {
  3. try {
  4. int a[] = new int[5];
  5. a[10]=9; // ArrayIndexOutOfBoundsException
  6. String s=null;
  7. s.length();
  8. }
  9. catch (ArrayIndexOutOfBoundsException e)
  10. {
  11. System.out.println("1st catch block guide2java");
  12. System.out.println(e);
  13. }
  14. catch (NullPointerException e)
  15. {
  16. System.out.println("2nd catch block guide2java");
  17. }
  18. catch (NumberFormatException e)
  19. {
  20. System.out.println("3rd catch block guide2java");
  21. }
  22. System.out.println("rest of the code...");
  23. }
  24. }
In the above example we might think that two exceptions are raised i.eArrayIndexOutOfBoundsException
and NullPointerException but we need to remember when an exception is raised in try block,it immediately
stops the execution in try and looks for appropriate catch block.Hence following lines after  are not executed
Note:
At a time only one exception is raised and at a time only one catch block is executed.
We can write multiple catch blocks but the order of catch blocks is important which we will discuss in next post.If you like the above post ,also try the related post .Please post your comments if you like the post, comment are always appreciated :) .

Comments

Popular posts from this blog

C Language Class 2 : A Simple C Program Hello World

How To Change the Icons Names On Android Homescreen

C programming video tutorial | Introduction to Programming Languages