Java 7 Introduced 2 brilliant features for Exceptions which can help to remove lots of boilerplate code in your project. These features are
1. Multi catch Exceptions
2. Multi throws Exceptions
Prior to Java 7 one catch block can handle only one exception.
public void method(){
try {
// Some code that can throw one of catch exceptions
} catch (Exception1 e) {
e.printStackTrace();
} catch (Exception2 e) {
e.printStackTrace();
} catch (Exception3 e) {
e.printStackTrace();
}
}
But sometimes you handle more than of these exception in similar way so you end up repeating same catch block code for all exception.
From Java 7 one catch block can handle multiple exceptions with the help of pipes. So the same code will become like:
public void method(){
try {
// Some code that can throw one of catch exceptions
} catch (Exception1 e | Exception2 e | Exception3 e) {
e.printStackTrace();
// If required you can define your exception handler and hadle exceptions as per there type in
handler
}
}
Second nice feature is multiple exceptions in throws section as method signature. Consider if you have just one Exception catch block for re-throw but you want to throw different type of exceptions to give user more granular access to thrown exception, prior to Java 7 you can not do so.
For example in below Example lets say if you want to tell user what ll type of exception this method can throw you can not do. You can only put top exception in hierarchy throw from method into method signature:
public void method(int i ) throws Exception { // Can not tell FirstException or SecondException
try {
if(i<0){
throw new FirstException();
} else {
throw new SecondException();
}
} catch (Exception e) {
throw e;
}
}
Earlier it was not allowed/possible to put FirstException and SecondException together in Throws section of method signature. But from Java 7 compile has started doing more inclusive type checking so you can do that now which makes your method signature much more clear:
public void method(int i ) throws FirstException, SecondException{
try {
if(i<0){
throw new FirstException();
} else {
throw new SecondException();
}
} catch (Exception e) {
throw e;
}
}
1. Multi catch Exceptions
2. Multi throws Exceptions
Prior to Java 7 one catch block can handle only one exception.
public void method(){
try {
// Some code that can throw one of catch exceptions
} catch (Exception1 e) {
e.printStackTrace();
} catch (Exception2 e) {
e.printStackTrace();
} catch (Exception3 e) {
e.printStackTrace();
}
}
But sometimes you handle more than of these exception in similar way so you end up repeating same catch block code for all exception.
From Java 7 one catch block can handle multiple exceptions with the help of pipes. So the same code will become like:
public void method(){
try {
// Some code that can throw one of catch exceptions
} catch (Exception1 e | Exception2 e | Exception3 e) {
e.printStackTrace();
// If required you can define your exception handler and hadle exceptions as per there type in
handler
}
}
Second nice feature is multiple exceptions in throws section as method signature. Consider if you have just one Exception catch block for re-throw but you want to throw different type of exceptions to give user more granular access to thrown exception, prior to Java 7 you can not do so.
For example in below Example lets say if you want to tell user what ll type of exception this method can throw you can not do. You can only put top exception in hierarchy throw from method into method signature:
public void method(int i ) throws Exception { // Can not tell FirstException or SecondException
try {
if(i<0){
throw new FirstException();
} else {
throw new SecondException();
}
} catch (Exception e) {
throw e;
}
}
Earlier it was not allowed/possible to put FirstException and SecondException together in Throws section of method signature. But from Java 7 compile has started doing more inclusive type checking so you can do that now which makes your method signature much more clear:
public void method(int i ) throws FirstException, SecondException{
try {
if(i<0){
throw new FirstException();
} else {
throw new SecondException();
}
} catch (Exception e) {
throw e;
}
}
No comments:
Post a Comment