Exceptions II

We will extend our discussion about ‘Exceptions’ which we had started in an earlier post. These were the key takeaways from our earlier post:

  1. Differences between checked and unchecked exceptions
  2. Important points regarding ‘finally’ clause
  3. Specific exceptions must be declared before the general exceptions

In this post, we will discuss the legal and illegal forms of ‘try-catch’ constructs and more ways to declare exceptions.

A.  Legal and Illegal forms of ‘try-catch’ constructs :

It is illegal to use ‘try-catch-finally’ clause without the ‘catch’ clause or the ‘finally’ clause. In other words, it is illegal to use the ‘try’ clause alone. When the ‘try’ clause is used alone, it will result in compiler error. Let us see an example to illustrate this:

package whizlabs;
 public class except_2 
 {
   public static void main(String[] args) 
 {
   int i=10,j; // TODO Auto-generated method stub
   try
   {
    j = i/10;
   }
  }
 }

In the above example, the ‘try’ clause is used alone without the ‘catch’ clause or the ‘finally’ clause. This results in a compiler error.

B. The ‘catch’ clause or the ‘finally’clause when used – must be used immediately after the ‘try’ clause. It is illegal to use it after several statements following the ‘try’ clause. The following program illustrates this:

package whizlabs;
  public class except_2 {
    public static void main(String[] args) {
      int i=10,j; // TODO Auto-generated method stub
A:    try{
         j = i/0;
       }
      System.out.println("Division by zero causes an exception");
B:    catch(ArithmeticException e){}
     }
  }

 

         The above program shows the presence of the following statement
“System.out.println(“Division by zero causes an exception”);”
        placed between point ‘A’ and point ‘B’(which is in between the ‘try-catch’) results in a compiler error.

    Consider this for a legal version of the ‘try-catch’ construct:

package whizlabs;
 public class except_2 {
   public static void main(String[] args) {
     int i=10,j; // TODO Auto-generated method stub
     try{
       j = i/0;
     }
      catch(ArithmeticException e){
        System.out.println("Division by zero causes an exception");
     }
   }
 }

This will result in the following output:
“Division by zero causes an exception”

C. Extending on point ‘a’, the ‘try’ clause can have either the ‘catch’ clause or the ‘finally’ clause or both. This is also a legal example in addition to the above example :

package whizlabs;
public class except_2 {
  public static void main(String[] args) {
    int i=10,j; // TODO Auto-generated method stub
    try {
       j = i/10;
     }
      finally{
         System.out.println("We are done");
      }
   }
}

 In the above example, we see the ‘try’ clause used along with the ‘finally’ clause.
The output of the above program as expected will be as follows:
We are done 

Declaring exceptions:
We saw the legal and illegal forms of ‘try-catch’ constructs. Next we will see how to declare exceptions. Consider the following example:

package whizlabs;
import java.io.*;
 public class except_2 {
   public void except() throws IOException
   {
     except_example();
   }
   void except_example() throws IOException{
    }
 }

The above example shows the way to throw an exception. In order to throw an exception, we use the ‘throws’ keyword along with the type of exception.
In the above example, the methods ‘except()’ and ‘except_example()’ throw ‘IOException’. Declaring a method throws an exception, is just an indication that it might throw an exception – it is not a necessity that it must throw the exception.
In the above example, it can also be noted that both methods are throwing the same exception. Any guesses on why it is so? We will discuss this and other things in yet another blog post.

Bibliography
SCJP 6. In B. B. Kathy Sierra.

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top