Multiple Exceptions In Java 7 New Concept

Introduction

 
In this article, we will discuss multiple exceptions in Java 7. It's a new feature released by Java 7 to improve performance. First, we discuss exceptions and exception handling and after that multiple exception in Java 7.
 

Exception in Java

 
In terms of dictionaries, an exception is an abnormal condition. When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.
 
In Java, an exception is an event that disrupts the normal flow of a program.
 
When we execute our program, sometimes an error is generated for some of the reasons given below, when this error is generated during execution it is called an exception. The exception can occur:
  • When the user has entered incorrect data or we can say invalid data.
  • When we opened a file that's needed in the program but can't be found.
  • Sometimes when the network connection is lost during the execution of the program.
To understand exceptions in Java, you need to understand the two categories of exceptions; they are:
  1. Checked Exception (also called a compile-time exception)
  2. Unchecked Exception (also called a run-time exception)

Checked Exception

 
It is also called a compile-time exception since they occur during compilation of the program. For example, sevletException, ClassNotFoundExp, NoSuchFieldException etc.
 

Unchecked Exception

 
It is also called a run-time exception since they occur during run-time. Such an exception is not checked at compile-time, in other words, NullPointerException, OutOfMemoryError, etc.
 
Example
  1. public class RunTimeExcpn   
  2.   {   
  3.     public static void main(String[] args)   
  4.       {   
  5.         String str = null;  
  6.         int len = str.length();  
  7.       }   
  8.   }  
Output
 
pic-1.jpg
 
Now the subject arises of how to overcome this problem of exception, for that Java provides am exception handler technique. Now we discuss exception handling in Java.
 

Exception Handling in Java

 
It is a powerful feature provided in Java. It is a mechanism to handle run-time errors so that the normal flow of the program is maintained.
 
Exception handling consists of the following five keywords:
  1. Try
  2. Catch
  3. Finally
  4. Throw
  5. Throws
Now, we show an example for exception handling:
  1. class Demo  
  2.   {  
  3.     public static void main(String args[])  
  4.       {  
  5.         try  
  6.           {  
  7.             int data=50/0;  
  8.           }  
  9.         catch(ArithmeticException e)   
  10.           {  
  11.             System.out.println(e);  
  12.           }  
  13.         System.out.println("remaining code");  
  14.       }  
  15.   }  
Output
 
In this program, an arithmetic exception occurs that is sometimes called "divide by null error" problem, for handling this exception we use a "try-catch block". In the try block, we write the code where the problem occurs and in the catch block, we catch the exception as determined by its type. In this program the arithmetic exception is generated so we use a catch(ArithmeticException e) statement to maintain the flow of the program.
 
pic-2.jpg
 
We are not going into the details of Exception Handling; our topic is "multiple exceptions in Java 7". So for discussing our topic we take a short look at "exception" and "exception handling" in Java.
 

Multiple Exception in Java 7 new concept

 
It provides a way of "Handling More Than One Type of Exception". Java 7 made it possible to catch various exceptions in the same catch block that is not possible in prior versions of Java. This is also known as multi-catch.
 
In prior versions of Java:
  1. catch (IOException ey)  
  2.   {  
  3.     logger.log(ey);  
  4.     throw ey;  
  5. catch (SQLException ey)   
  6.   {  
  7.     logger.log(ey);  
  8.     throw ey;  
  9.   }  
  10. catch(Exception e)   
  11.   {  
  12.     logger.severe(e);  
  13.   }   
In prior versions of Java 7, it is difficult to create a common method to remove the repeated code because the variable "ey" has various types.
 
The following example is valid in Java 7 and later, it eliminates the need for repeated code: 
  1. catch (IOException | SQLException ey)   
  2.   {  
  3.     logger.log(ey);  
  4.     throw ey;  
  5.   }  
The catch defines the types of exceptions, and each exception type is separated with a vertical bar ( | ). 
 
Advantages of Multiple Exception in Java 7
  1. Single-line catching might help in JDBC, formatting date and Java IO in Java. For throwing an exception in IO related code we use IOException, in JDBC code we use SQLException, and for date formatting we use ParseException, that can't be handled in a single catch block prior to Java 7. Catching java.lang.Exception works for all kinds of exceptions but it is not a good practice. With multiple exceptions in Java 7 we catch both exceptions in one catch block.
  2. The exception handling changes in Java SE 7 allow you to program more concisely.
  3. They also allow you to partially handle an exception and then let it bubble up.
  4. Reduce a substantial amount of workload.
  5. Remove redundancy of code since we don't need to write a catch statement again and again.
Now in the following example, we show a difference between exception handling in Java 7 and in prior versions of Java.
 

Exception handling in Java

  1. class ExceptionEx1  
  2.   {  
  3.     public static void main(String args[])  
  4.       {  
  5.         try  
  6.           {  
  7.             int a[]=new int[5];  
  8.             a[5]=345/0;  
  9.           }  
  10.         catch(ArithmeticException e)  
  11.           {  
  12.             System.out.println("work 1 done");  
  13.           }  
  14.         catch(ArrayIndexOutOfBoundsException e)  
  15.           {  
  16.             System.out.println("work 2 completed");  
  17.           }  
  18.         catch(Exception e)  
  19.           {  
  20.             System.out.println("Common task completed");  
  21.           }  
  22.         System.out.println("remaining part of the code");  
  23.       }  
  24.   }  
Output
 
pic-3.jpg
 

Exception handling in Java 7 

  1. class ExceptionEx2  
  2.   {  
  3.     public static void main(String args[])  
  4.       {  
  5.         try  
  6.           {  
  7.             int a[]=new int[5];  
  8.             a[5]=345/0;  
  9.           }  
  10.         catch(ArithmeticException | ArrayIndexOutOfBoundsException e)  
  11.           {  
  12.             System.out.println("work 1 done");  
  13.           }  
  14.         catch(Exception e)  
  15.           {  
  16.             System.out.println("Common task completed");  
  17.           }  
  18.         System.out.println("remaining part of the code");  
  19.        }  
  20.   }  
Output
 
pic-4.jpg
 
Now, from the example above I hope it might be clear what the basic difference is in exception handling in Java and in Java 7. The basic difference is of coding style (in other words removal of code redundancy), that can be removed in Java 7. Look in the example, there is a catch block, in prior versions of Java we need to repeat catch blocks again and again but in Java 7 this problem has been resolved. In Java 7 we must use a single catch instead of multiple catches.


Similar Articles