Multi Catch Blocks for Exceptions
If you are keeping an eye on the new features coming with JDK 7 then probably you could have also heard about multi catch blocks.
They are basically a new way to do things. The purpose of these blocks is to handle multiple exceptions. In pre JDK 7 world, if you wanted to catch two different exceptions but wanted to execute same code in the catch blocks then either you need two catch blocks with repeated code or a finally block.
The problem with finally block is that it gets executed even if no exception occurs which means we need to repeat code in catch blocks for printing stack trace or to re-throw exceptions.
But with JDK 7, if want to catch SQLException and IOException both with same code then I can write:
I have tried this new feature of JDK 7 with the release milestone M12. The multi catch feature is a part of project Coin which includes other features being introduced in JDK 7. Following is the code I had written:
Though I could not compile this code with Eclipse Helios, but I could successfully compile it using command line.
The variable ex is an instance of ArrayIndexOutofBoundsException because it is declared with it. I have written the following program to verify it:
If you are keeping an eye on the new features coming with JDK 7 then probably you could have also heard about multi catch blocks.
They are basically a new way to do things. The purpose of these blocks is to handle multiple exceptions. In pre JDK 7 world, if you wanted to catch two different exceptions but wanted to execute same code in the catch blocks then either you need two catch blocks with repeated code or a finally block.
The problem with finally block is that it gets executed even if no exception occurs which means we need to repeat code in catch blocks for printing stack trace or to re-throw exceptions.
But with JDK 7, if want to catch SQLException and IOException both with same code then I can write:
try{
try-code
}
catch (SQLException | IOException) {
catch-code
}
I have tried this new feature of JDK 7 with the release milestone M12. The multi catch feature is a part of project Coin which includes other features being introduced in JDK 7. Following is the code I had written:
package com.example;
public class Test{
public static void main (String args[]){
try {
if (args[0].equals("null")) {
throw (new NullPointerException());
} else {
throw (new ArrayIndexOutOfBoundsException());
}
} catch (NullPointerException | ArrayIndexOutOfBoundsException ex) {
ex.getMessage();
}
}
}
Though I could not compile this code with Eclipse Helios, but I could successfully compile it using command line.
The variable ex is an instance of ArrayIndexOutofBoundsException because it is declared with it. I have written the following program to verify it:
public class Main{
public static void main(String[] args) {
System.out.println("hello");
try {
if (args[0].equals("null")) {
throw (new NullPointerException());
} else {
throw (new ArrayIndexOutOfBoundsException());
}
} catch (NullPointerException | ArrayIndexOutOfBoundsException ex) {
System.out.println(ex instanceof NullPointerException);
System.out.println(ex instanceof ArrayIndexOutOfBoundsException);
ex.printStackTrace();
}
}
}
Ran it with the command:
java -classpath c:\com\example Main
And the output is:
hello
false
true
java.lang.ArrayIndexOutOfBoundsException: 0
at Main.main(Main.java:5)
What class does ex start out as when you first use it? I'm also curious as to how IDEs will handle auto-complete for it.
ReplyDeleteIndeed, the less strict typing is there, the worse code completion is. However, I think that this is not the case for the "multi catch block". If you want to use interfaces specific to some exceptions' - you should probably use separate catch blocks for them. I understand that the whole idea of "multi catch block" is that you're going to process listed exceptions in some generic manner, therefore using their common interface (the one from Throwable most likely). So I think Throwable interface will be used in code completion, or maybe the "nearest" common parent, if the IDE is smart enough :)
ReplyDeleteBut how I will know which exceptions was thrown? SQLException or IOException
ReplyDeletewe can use root Exception class to catch all the exception....like...
ReplyDeletepublic static void main (String args[]){
try {
if (args[0].equals("null")) {
throw (new NullPointerException());
} else {
throw (new ArrayIndexOutOfBoundsException());
}
} catch (Exception ex) {
ex.getMessage();
}
}
So, I find actual no use of it.... :-|
That's the point. What if you don't need to catch ALL exceptions (which is by the way a bad practice and is really required in a few places in the system only), but only need to handle few specific exceptions, but handle them exactly the same way (like, log a message, or show a message to user, whatever). That's why.
ReplyDeleteThe variable ex is an instance of ArrayIndexOutofBoundsException because it is declared with it. I have written the following program to verify it:
ReplyDeletepublic class Main{
public static void main(String[] args) {
System.out.println("hello");
try {
if (args[0].equals("null")) {
throw (new NullPointerException());
} else {
throw (new ArrayIndexOutOfBoundsException());
}
} catch (NullPointerException | ArrayIndexOutOfBoundsException ex) {
System.out.println(ex instanceof NullPointerException);
System.out.println(ex instanceof ArrayIndexOutOfBoundsException);
ex.printStackTrace();
}
}
}
Ran it with the command:
java -classpath c:\com\example Main
And the output is:
hello
false
true
java.lang.ArrayIndexOutOfBoundsException: 0
at Main.main(Main.java:5)
Well, this is because your program indeed throws ArrayIndexOutOfBoundsException. What if you try running it as:
ReplyDeletejava -classpath c:\com\example Main null
?
Great tutorial, I was not aware of this. this looks to me as pretty decent feature instead of putting catch block for every exception. Thanks for this.
ReplyDeleteJavin
How Classpath works in Java
@2b4bcfaf1d7e50abce9aa82f620a51e5:disqus the output is same with null as the argument.
ReplyDeleteI was not aware of this feature as well. Looks great. It would be pretty useful and would shorten long catch constructs. Doesn't look very complex as well.
ReplyDelete"The variable ex is an instance of ArrayIndexOutofBoundsException because it is declared with it."
ReplyDeleteThat's not true. In your program, (x instanceof ArrayIndexOutOfBoundsException) is true because you are not passing "null" to the main method. if you pass "null" as argument, the type of x will be NullPointerException.
In fact, when Java compiles multi-catch code, it converts the multiple catch statements into a single catch statement with closest common super class. In your code, it will be RuntimeException. So, compile time type of exception caught will be RuntimeException and at run time, the exact type depends on the exception thrown.