Improved and Better Exception Handling from Java 7

So as a continuation of my previous post Features from Java-7 that you will love being a developer, today I am going to introduce the very new Improved Exception Handling in Java SE 7.

I)Mutiple Exceptions handling
Before the launch of Java SE 7 we were habitual of writing code with multiple catch statements associated with a try block.
A very basic Example:
try {
// some instructions
}
catch(ATypeException e) {
}
catch(BTypeException e) {
}
catch(CTypeException e) {
}

But now with the latest update on Java, instead of writing multiple catch statements we can handle multiple exceptions within a single catch clause. Here is an example showing how this feature can be achieved.

try {
// some instructions
}
catch(ATypeException|BTypeException|CTypeException ex) {
throw e;
}

So multiple Exceptions in a single catch clause not only simplifies the code but also reduce the redundancy of code.

Essential Points to be noted:
  • In the case of multiple Exception parameters within a single catch clause, the parameter is implicitly final means the exception ex cannot be assigned a new value.
II)Intelligence of Throws clause

Method declaration includes the exceptions which the method is going to throw in its throws clause. And these exceptions in the throws clause are nothing but the exceptions thrown within the catch clause

So till now the approach we follow is

/* Let us assume that Exception is the base class and all other exceptions extend this class*/
public void demoMethod() throws Exception {
try {
// some instructions
throw new ATypeException();
}
catch(Exception e) {
throw e;
}
}


So in the above example the exception thrown is of Exception type. But now with Java 7 you can be more specific throwing an exception. Instead of Exception(Super class or base class) type, we can directly use ATypeException in the throws clause. In previous version of java it would have thrown a compile time error("unreported exception Exception ") for the above code.

Case which to not follow the above mentioned feature
If we assign a new value to the exception e within the catch clause then we need to throw the exception of type Exception instead of being specific.

Behind the Scenes
So what were the changes that were required to implement this feature?
First of all the grammar of the language was changed and multiple exceptions were allowed as parameters within the catch clause with the 'OR' operator as a separator.

Wasn't there any case where this implementation contradict the previous version of Java code?
Yes there is a case which should have been handled in the earlier version and which can break your previous code if we compile it again with the latest java updates.
If we have nested try catch and the inner catch clause throws an exception that is not exactly the type which is thrown to the outer catch then in such scenario you will be getting a compile time exception with Java 7 but it works well with previous version of java.

Example:
/* Let us assume that Exception is the base class and all other exceptions extend this class*/

try {
throw new A();
}
catch(Exception e) {
try{
throw e;
}
catch(B b) { // The code will never reach here.
}
}

In the above example Exception e can be of A type but can never be of B type. So the inner catch with B parameter is never reached.

Next Feature: Why do we need Type Inference from Java 7?

Related

java 8705755411833360008

Post a Comment

  1. Nice Post. This seems to be a very good feature

    ReplyDelete
  2. so pity cursor icon

    ReplyDelete

emo-but-icon

Translate

 

Recent Posts

comments

Recent Comments Widget

Join Us

 

Sponsored By

Recommended for you

get social

item