Java 7 Re-throw of an Exception

In this tutorial we are going to see re-throw exception in Java 7.

In previous versions of Java re-throwing exception in catch block didn’t indicate the actual exceptions possible from the try block.Also you could not change the type of exception thrown in the catch block without changing the method signature.

Now in Java 7, the semantics for catching and throwing exceptions has been redefined.If you catch an exception of some type, and don’t assign the exception variable, the compiler will copy over the checked exception type that can be thrown from try block.

If you want to read more about Java 7 Try statement then read my previous articles about Java 7 Try with resource and Java 7 try with multi catch statements

Old way to Re-throw

public static void main(String[] args) throws IOException,SQLException {
  // TODO Auto-generated method stub
  
  
   try {
    readFileSaveInDatabase();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    System.err.println(e.getMessage());
    throw e;
   }catch (IOException  e) {
    // TODO: handle exception
    System.err.println(e.getMessage());
    throw e;
   }
}

Consider the above code in that we are throwing IOException and SQLException. In that we are logging each type of exception being thrown by try block before re-throwing them.So there is duplication of code in catch block.So before Java to avoid the code duplication we can write the code as follows

public static void main(String[] args) throws Exception{
  // TODO Auto-generated method stub
  
  
   try {
    readFileSaveInDatabase();
   } catch (Exception e) {
    // TODO Auto-generated catch block
    System.err.println(e.getMessage());
    throw e;
   }
   
  

 }

But, in above code we require to re throw the exception of type java.lang.Exception from the calling method

Java 7 Re-throw Exception

public static void main(String[] args) throws IOException,SQLException {
  // TODO Auto-generated method stub
  
  
   try {
    readFileSaveInDatabase();
   } catch (final Exception e) {
    // TODO Auto-generated catch block
    System.err.println(e.getMessage());
    throw e;
   }
   
  

 }

In above code the keyword final is used in catch clause. When a parameter is declared final, the Java compiler statically knows to only throw those checked exception that were thrown by try block and were not caught in any preceding catch blocks.


Posts You may Like

  1. JSON Tree Model in Jackson
  2. Java Best Practice – White Spaces
  3. XML SAX Parser Introduction
  4. How to write JSON using GSON Streaming
  5. Java Best Practice – File Organization
  6. Java 7 Try statement with resources
  7. String Concatenation in Java
  8. Log4j Architecture
  9. How to add value to all types of Collections
  10. How to write file using FileOutputStream in Java
  11. Java XML Introduction
  12. Java Tutorial – Java Environment Setup in Windows
  13. How to Convert JSON String to Object using GSON
  14. How to make read-only file in Java
  15. How to Read JSON using GSON Stream

Be the first to comment

Leave a Reply

Your email address will not be published.


*