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.


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java 7 Re-throw of an Exception
  2. Java 7 Binary Literals
  3. Java 7 Switch statement on String
  4. Java 7 Try statement with resources
  5. Java 7 Underscores in Numeric Literals
  6. Java 7 new features
  7. Java 7 Try statement with multi catch

Be the first to comment

Leave a Reply