Nov 8, 2012

Java 7 Overview: Multi-Catch Support

Java 7, also known as Project Coin also improved Java's exception handling with the introduction of Muti-Catch Exception and Final Rethrow supports. With Multi-catch you will now be able to catch multiple exceptions in single block contrary to Java 6 or earlier where this isn't possible. In the previous posts, I used a multicatch feature in the Cryptography examples. Despite the fact that in the previous code, the method encrypt_data() throws an exception.


I repeat the same code here, but without letting the method throws any exception, this allow me to catch even more exceptions using the new multicatch syntax. 

The following encrypt_data() method catches three types of exceptions in single catch block, separating them with a vertical bar a '|' operator.
private static byte[] encrypt_data(String plain_text) 
 {
         byte[] encrypted = null;
        try {
            //Re-initialize the cipher  for decription
            cipher.init(Cipher.ENCRYPT_MODE, secret_key);
            
            //Do the encryption
            encrypted = cipher.doFinal(plain_text.getBytes());
           
        } 
         catch (IllegalBlockSizeException | BadPaddingException | 
                  InvalidKeyException ex) {
            System.out.println("Error! " + ex.getMessage());
            throw ex;
        }
        return encrypted;
    }
The following code featured the same encrypt_data() method but catches all the three exceptions using the pre Java 7 catch pattern. All the three exceptions have to be caught in a separate block of code with the 'catch' keyword.
   private static byte[] encrypt_data(String plain_text) 
    {
         byte[] encrypted = null;
        try {
            //Re-initialize the cipher  for decription
            cipher.init(Cipher.ENCRYPT_MODE, secret_key);
            
            //Do the encryption
            encrypted = cipher.doFinal(plain_text.getBytes());
           
        } 
         catch (InvalidKeyException ex) {
            System.out.println("Error! " + ex.getMessage());
            throw ex;
        }
         catch (BadPaddingException ex) {
           System.out.println("Error! " + ex.getMessage());
           throw ex;
        } 
         catch (IllegalBlockSizeException ex) {
            System.out.println("Error! " + ex.getMessage());
            throw ex;
        }
        return encrypted;
    }
The other feature related to this is the Final Rethow. It allows you to catch an exception of some type as well as it’s subtype and then rethrow it without having force you to add a throws clause to the method signature.
Here is syntax:
try {
      // some code
     } catch (final Exception ex) {
      ex.getMessage();
  } 
This feature is controversial and is being considered as undesirable by many. Many argued that adding final keyword to the throwable is too subtle and unconnected to the task at hand. The issue with exceptions and issues with final rethrow was discussed here extensively.

Related Posts 

Java 7 Overview: String in Switch Support 

Java 7 Overview: Automatic Resource Management

No comments:

Post a Comment