Improved Exception handling in Java 7

Java 7 has improved the exception handling by ease of development, reduced code duplication  and intelligent throws clause.

1. More than one  exception types in single catch block: Java 7 onwards single catch block can handle multiple exception types. Now Catch block can specify ‘|’ separated exception types instead of only one exception. It prevent code duplication in case when same operation has to perform for all of set of types of exceptions and improve code readability.

Old style :

try{
    //your code here
}catch (IOException ex) {
     log(ex);
     throw ex;
catch (SQLException ex) {
     log(ex);
     throw ex;
}


New style:


try{
    //your code here
}catch (IOException|SQLException ex) {
     logger.log(ex);
     throw ex;
}


2. Intelligent throws clause : Method declaration has to include the list of exception its is going to throws using throws clause. These exception types has to be matched with catch block throw statement. Java 7 onwards compiler is more intelligent to analysis the code and allows to declares exception types in method throws declaration  based on following condition.



  • The try block is able to throw it.

  • There are no other preceding catch blocks that can handle it.

  • It is a subtype or supertype of one of the catch clause's exception parameters.

For example, consider code below



public void test1()throws Exception {
        try{
            File f = new File("sample");
            f.getAbsolutePath();// throws SecurityException exception
        }catch (Exception exp){//catching super type
            throw exp;//throwing Exception
        }
    }


In above code f.getAbsolutePath()throws SecurityException but catch block parameter is Exception ( Super type). As catch block is throwing exception of type Exception, throws clause in method declaration has mention Exception.


In Java7, same code you can write like below



 
public void test1()throws SecurityException {
        try{
            File f = new File("sample");
            f.getAbsolutePath();// throws SecurityException exception
        }catch (Exception exp){//catching super type
            throw exp;//throwing Exception
        }
}


Here test1 method declares SecurityException instead of Exception. Java 7 compiler allows you specify exception type SecurityException in throws  clause of method test1 because you can throw a exception that is super type of any of the type declared in the throws .


Here compiler knows exception of type SecurityException is being throw from try block.


Reference :


http://download.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html

Comments

  1. The second one is really interesting!

    ReplyDelete
  2. Yogesh I didn't understand the second one.
    What if if f.getAbsolutePath() throws IOException. Your catch block would catch it and throw it right?
    But your throws class doesn't have IOException it.
    Am I missing something?

    ReplyDelete

Post a Comment

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester