views:

55

answers:

4

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.

+6  A: 

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.

Kobi
But the type of Excp2 is Exception(as it extends Exception class). The fix that you provided is working fine, but I don't understand the use of `Excp2 excp2`, doesn't that mean the <b>type</b> is Excp2?Also, what's the use of `log(excp2)`?
MoonStruckHorrors
@MoonStruckHorrors - `log` is just to demonstrate how it is use. I think you're missing something basic in **inheritance**, and exception are a poor way of starting to learn this extremely important subject. In a nutshell, it is true that you throw an `Exception`, but is it a little *more true* that you are throwing an `Excp2` - this makes a difference to Java.
Kobi
I took your word and tried to understand it from the scratch. You were right, I was overlooking the basics of inheritance. I tried it in a different way. Instead of using `throw new Excp()`, I used a longer method. `Excp xyz = new Excp(); throw xyz;`. I finally understood that `Excp` is just like a normal class and by using `new Excp()` we're just creating a new instance/object of it. Thanks.
MoonStruckHorrors
A: 

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 :)

steve
Nope. Multiple exceptions can be caught. Hence, the purpose of this thread is valid.
MoonStruckHorrors
An exception can be thrown, caught, shipped around between methods, re-thrown and caught again as often as you like. The OP's error had nothing to do with catching the same exception multiple times, but with his confusion of the `Exception` class with the general concept of an exception. His code ended up checking twice for the same exception class.
Carl Smotricz
+1  A: 

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.

sarnold
A: 

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.

npinti