Posts

Showing posts from August, 2011

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 i