Reputation: 730
Here's the code:
public class Exc {
int x = 2;
public void throwE(int p) throws Excp, Excp2 {
if(x==p) {
throw new Excp();
}
else if(x==(p+2)) {
throw new Excp2();
}
}
}
Here's the handler code:
public class tdExc {
public static void main(String[] args) {
Exc testObj = new Exc();
try {
testObj.throwE(0);
System.out.println("This will never be printed, so sad...");
} catch(Exception Excp) {
System.out.println("Caught ya!");
} catch(Exception Excp2) {
System.out.println("Caught ya! Again!!!!");
} finally {
System.out.println("This will always be printed!");
}
}
}
Excp
and Excp2
both extends Exception
and have similar code(nothing). Now I'm getting the error Exception has already been caught
error at Excp2
, regardless of whether I supply 2 or 0 to throwE
method.
Upvotes: 5
Views: 17158
Reputation: 52185
When you are catching an exception, you have to specify what type of exception you are catching, this will allow you to better handle the exception that has occured. One thing that you have to keep in mind though, is that there is that there are specific and other more "wide purpose" exceptions.
For instance, NumberFormatException
is more specific than Exception
, since NumberFormatException will be thrown whenever you will try to parse a string into a number.
Thus, when having multiple catch statements, always put the most specific one on top, and the more generic ones at the end. If you put the more generic ones at the beginning, they will catch the exception before it can be passed to a more specific catch statement.
In your case, you are trying to catch the same exception twice, since you have two catch statements that try to catch the same exception.
Upvotes: 2
Reputation: 137997
You're looking for:
try
{ }
catch(Excp excp)
{
log(excp);
}
catch(Excp2 excp2)
{
log(excp2);
}
finally
{ }
When you catch an exception, to specify the type of the exception, and the name of of its reference.
Your original code tried to catch Exception
, which is the least specific exception, so you cannot catch anything after that.
Upvotes: 10
Reputation: 104020
Java dispatches to the catch() clauses based on the types of the exception: your clauses are both trying to catch an exception of type Exception
, and give them the names Excp
and Excp2
:
public class tdExc { public static void main(String[] args) { Exc testObj = new Exc(); try { testObj.throwE(0); System.out.println("This will never be printed, so sad..."); } catch(Exception Excp) {
Shouldn't this be Excp e
?
System.out.println("Caught ya!"); } catch(Exception Excp2) {
Shouldn't this be Excp2 e
?
System.out.println("Caught ya! Again!!!!"); } finally { System.out.println("This will always be printed!"); } } }
And, while it's unrelated, I think your earlier code would be easier for you to think about if you write it more like this:
public void throwE(boolean p) throws Excp, Excp2 { if(p) { throw new Excp(); } else { throw new Excp2(); } }
Call it with true
or false
as parameters.
Upvotes: 1
Reputation: 21499
I believe the exception can only be caught once with java. The first exception handler will process the error.
Please someone tel me if this is true for java :)
Upvotes: -1