5

Look at the code below. Although the catch clause itself throws an exception, the finally block's return statement causes that exception to be swallowed. This method will return 420 even if something goes wrong in the catch block.

private static int foo() throws Exception
{
    try {
        throw new Exception("try");

    } catch (Exception ex) {
        throw new Exception("catch");
    } finally {
        String s = "";
        return 420;
    }


}
Foo
  • 4,206
  • 10
  • 39
  • 54
  • 3
    You've described what it does, what's your question again? Is it dangerous? Are the `Exception`s dangerous? – Elliott Frisch Jan 18 '16 at 22:42
  • 2
    Define "dangerous" in this context. What you've shown is legal and would have the same result if the return statement was placed after the try/catch/finally. There is no harm in doing this, so what do you mean by asking if it is "dangerous" ? – Reid Spencer Jan 18 '16 at 22:48
  • 1
    It's save. https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html – CyberAleks Jan 18 '16 at 22:49
  • 1
    What's the point? If you throw an exception then the call won't be able to interrupt it. – MadProgrammer Jan 18 '16 at 22:49
  • 1
    My point was the same as the point, that this question is marked a duplicate off. But I guess too many people on here are too eager to humiliate a person if they think the question is stupid. Although many times the question is not the one that is stupid. Based on the answer in the duplicate questions, yes it is a bad practice to put a return in the finally clause. – Foo Jan 18 '16 at 22:56
  • Another possible duplicate:[return in try-catch's finally block](http://stackoverflow.com/questions/19498561/return-in-try-catchs-finally-block-in-java-is-there-any-good-point-in-this-exa?lq=1). – Mick Mnemonic Jan 18 '16 at 22:56
  • I think it's a relevant/good question, but it has also been asked a few times before. – Mick Mnemonic Jan 18 '16 at 22:59
  • @Mick: Look at Elliot's comment. – Foo Jan 18 '16 at 23:00

3 Answers3

1

You should return something else if you encounters an Exception. The exception is only dangerous if the variable that threw that exception is used in the final return statement. Consider this:

int a;
try{
 //...
}catch(Exception e){
 //a throws an exception somehow
}finally{
 returns a;
}

And when you use a on the other side like this:

a += 1;

You get a dangerous exception.

My suggestion is to do this:

try{
 //...
}catch(Exception e){
 //a throws an exception somehow
 returns -1;
}finally{
 returns a;
}

And on the other side:

if(return_value == -1) // skip or do something else;

This way, you won't get a unpredicted exception on the other side.

sguan
  • 1,039
  • 10
  • 26
1

Return in finally is a very bad idea. It doesn't hide only the exceptions you throw yourself, but also virtual machine errors such as stack overflow or out of memory errors. These errors can be thrown at any point, including when the key invariants of data structures don't hold, and it will be impossible to predict what the program will do.

Joni
  • 108,737
  • 14
  • 143
  • 193
0

In your case it is safe, but if we change your scenario a little

private static FileReader foo() throws Exception{
    try {
        throw new Exception("try");
    } catch (Exception ex) {
        throw new Exception("catch");
    } finally {
        return new FileReader("");//this may also throw something
    }
}

Now, because we didn't specify proper path in your file system return new FileReader(""); will throw FileNotFoundException and we will lose exception thrown in catch section with new Exception("catch"); which is potentially dangerous.

Pshemo
  • 122,468
  • 25
  • 185
  • 269