Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java catch block, caught exception is not final

I am checking out the new features of Java SE7 and I am currently at this point:

http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html

regarding the catch multiple feature, when I came across this statement:

Note: If a catch block handles more than one exception type, then the catch parameter is implicitly final. In this example, the catch parameter ex is final and therefore you cannot assign any values to it within the catch block.

I never noticed that the caught exception is not final in the classic case of handleing caught exceptions.

I just wonder why is that a good thing in the first place? Would it not be ill-advised to essentially MODIFY a caught exception before I guess rethrowing it or maybe logging it's message? Should it not be up to the trowing mechanism to create the exception so it represents exactly what it should?

I have never seen an exception being modified in the catch block can maybe someone point out it's benefits?

like image 939
Peter Jaloveczki Avatar asked Jun 06 '13 10:06

Peter Jaloveczki


People also ask

What happens if exception is thrown in catch block?

Answer: When an exception is thrown in the catch block, then the program will stop the execution. In case the program has to continue, then there has to be a separate try-catch block to handle the exception raised in the catch block.

Can a catch block throw the exception caught by itself?

Q29)Can a catch block throw the exception caught by itself? Ans) Yes. This is called rethrowing of the exception by catch block. e.g. the catch block below catches the FileNotFound exception and rethrows it again.

Is finally called if catch throws exception?

A finally block always executes, regardless of whether an exception is thrown. The following code example uses a try / catch block to catch an ArgumentOutOfRangeException. The Main method creates two arrays and attempts to copy one to the other.

What is final exception in Java?

The finally block in java is used to put important codes such as clean up code e.g. closing the file or closing the connection. The finally block executes whether exception rise or not and whether exception handled or not. A finally contains all the crucial statements regardless of the exception occurs or not.


2 Answers

It's pretty much the same as method arguments:

You usually don't modify them and many people agree that they should be treated as final (whether or not to actually write final in front of them is a matter of some debate).

But since there's no technical requirement that says it must be final, the language gives you the option to choose.

Personally I know of no good reason to modify the exception reference of a catch-block.

like image 186
Joachim Sauer Avatar answered Oct 23 '22 11:10

Joachim Sauer


I cannot think of a convincing use-case for modifying an exception in a classic catch clause. However, that doesn't mean it should be forbidden. Especially given that you can modify a parameter variable. If you find this worrisome, you have the option of declaring the exception variable to be final.

On the other hand, allowing modification in the multi-exception catch would introduces the possibility of truly bizarre and confusing code such as this:

  catch (IOException | NullPointerException ex) {
      ...
      ex = new IllegalArgumentException(...);
  }

I imagine that's what the designers had in mind when they added the restriction in this case.

But either way, this is how the Java language is defined, and what we have to live with. There's not a lot of point in debating the apparent inconsistencies ... unless you are intending to design and implement a new language.

like image 29
Stephen C Avatar answered Oct 23 '22 11:10

Stephen C