20

say I have the following

try{
//something
}catch(Exception generic){
//catch all
}catch(SpecificException se){
//catch specific exception only
}

What would happen when it comes across SpecificException ? Does it catch it as a generic exception first, and then catch the specificexception ?

Or does it only catch SpecificException while ignoring generic exceptions.

I don't want both generic and specificexception being caught.

1
  • Question contains code would produce compile time error. General exception should be after specific exception. Commented Jan 15, 2017 at 9:11

6 Answers 6

25

This won't compile. You'll be told that the specific exception block isn't reachable.

You have to have the more specific exception catch block first, followed by the general one.

try
{
    //something
} 
catch(SpecificException se)
{
    //catch specific exception only
}
catch(Exception generic)
{
    //catch all
}
Sign up to request clarification or add additional context in comments.

8 Comments

Imagine that the compiler allowed us to write exceptions in any order. What difference would it make ? I don't see any difference. Then, why enforce an order ?
Imagine that I were a rich man, 20 years younger. What difference would it make? Imagine all you want, but that's how the JDK works.
With that logic, why even bother finding reasons for anything ? Why does java not allow multiple inheritance ? Who cares ? It just works that way. See the point I am trying to make ? Yes, this my question is not important or critical. But it would be nice to know the answer. Chenqui.
No, there's plenty of documentation explaining why Java does not allow multiple inheritance of implementation (MI for interfaces is perfectly okay). This question is 3.5 years old. What are you adding with these comments? Nothing that I can see. Go find a better way to boost your 814 rep than being a comment troll.
Don't know you what you mean by this. Good thing for you about no points, because I'd vote this down if I could. Funny that you seemed to single out my answer when ALL THE OTHERS SAY THE SAME BLOODY THING. Go learn how to code.
|
16

No. All exceptions would be caught by the first block. The second will never be reached (which the compiler recognizes, leading to an error due to unreachable code). If you want to treat SpecificException specifically, you have to do it the other way round:

}catch(SpecificException se){
//catch specific exception only
}catch(Exception generic){
//catch all
}

Then SpecificException will be caught by the first block, and all others by the second.

8 Comments

I don't understand why this is the accepted answer. Trying to catch exceptions in the order shown in the original question results in a compile error, so the statement "All exceptions will be caught by the first block" is not applicable.
has it been checked with javac? In eclipse it fails, but javac might just issue a warning.
I thought Michael was referring to the code that he posted, in which case his statement is correct.
I am aware of the below answer with 12 votes. However, at previous given time where I read the below answer by duffymo it was much more shorter than this answer which I have currently chosen. This chosen answer was eye catching as it directly answered my question and pointed out the specific location of where I was wrong in my code. Then it proceeded to what would result if I let my original code execute. I like the ordering layout format of the question a great deal. All other answers provided are equally great but I tend to be contrarian by instinct. Thank you all.
@heysup: I think the point is that you couldn't have let your original code execute, because the compiler would have reported an error. A compile error means no .class file is created, so no code to run.
|
6

This does not compile with eclipse compiler:

Unreachable catch block for IOException. It is already handled by the catch block for Exception

So define them the other way. Only the specific one will be caught.

Comments

4

The catch blocks are tried in order, and the first one that matches the type of the exception is executed. Since Exception is the superclass of all exception types, it will always be executed in this instance and the specific cases will never be executed. In fact the compiler is smart enough to notice this and raise a compilation error.

Just reorder the catch clauses.

Comments

2

As a side note, the only way to have both catch blocks called is to use nested exceptions.

try {
  try{
  //something
  }catch(SpecificException se){
  //catch specific exception only
  throw se;
  }
}catch(Exception generic){
//catch all
}

1 Comment

I was afraid of this being the only way :(
0

My proposition - catch SQLException and check code.

try {
    getConnectionSYS(dbConfigFile, properties);
} catch (SQLException e){
    if (e.getErrorCode()==1017){
        getConnectionUSER(dbConfigFile, properties);
    } else {
        throw e;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.