Friday 11, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 28 December 2013

Java7 – More precise Re-throw of an Exception

In this tutorial we are going to see re-throw exception in Java7.
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 Java7,  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 complier will copy over the checked exception type that can be thrown from try block.
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

Java7 Re-throw

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 complier statically knows  to only throw those checked exception that were thrown by try block and were not caught in any preceding catch blocks.

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Java 7 – Try statement with multi catchIn this tutorial we are going to see java7 multi catch for similar type exceptionHistorically, catching multiple exceptions results in a catch block for each exception, with each block containing a variable with the type of t… Read More
  • Java7 – Binary LiteralsIn this tutorials we are going to see the integral types (byte, short, int, and long ) in  java7.This tutorials is created by referring Oracle official web site.Some content is taken from there.  In Java7, the integ… Read More
  • Java7 – Underscores in Numeric LiteralsIn this tutorial we are going to see underscores in numerical literals.Note: In this tutorial some of code taken from Oracle official web siteIn Java7 any number of underscore character (_) can appear in anywhere between nume… Read More
  • Java7 Features In this Tutorials we are going to see what are the changes and new features comes in Java7. Virtual Machine : JSR 292: Support for dynamically-typed languages (InvokeDynamic) Extensions to the JVM, the Java language, … Read More
  • Java 7 – Try statement with resourcesIn this tutorial we are going to see new features added to try-catch in Java 7. Resources in try statementOne of the most important feature added in java 7 is auto closing of resources like InputStream, BufferedInputStream,… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java7 – More precise Re-throw of an Exception Rating: 5 Reviewed By: eHowToNow