-5

I know that try statement is useless without catch or finally, and the finally clause is optional in a try-catch block. However, when I write a try statement without catch or finally, the compiler suggests inserting finally clause to complete try statement.

For example:

try {
    for (int j = 0; j <= i.length; j++) {
            System.out.println(i[j]);
    }
} catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("Catch");
} //no errors

try {
    for (int j = 0; j <= i.length; j++) {
        System.out.println(i[j]);
    }
} //syntax error

Error code:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Syntax error, insert "Finally" to complete TryStatement

at Driver.main(Driver.java:12)

Why is finally the only recommended statement to implement? Why not catch?

"I might want something to happen despite an exception being thrown, so I may not be looking to handle a specific exception in a specific way, but I may want to ensure that at least SOMETHING generic happens. If I can't handle it, at least do something." Looking for somebody to confirm on this.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
John Doe
  • 55
  • 1
  • 8
  • 8
    If there's neither `catch` nor `finally`, why would you put a `try` ? – Denys Séguret Jan 27 '16 at 14:46
  • 1
    http://stackoverflow.com/questions/2614473/does-it-make-sense-to-do-try-finally-without-catch – exception1 Jan 27 '16 at 14:49
  • so try exists if and only if catch or finally does? So they're interdependent.. – John Doe Jan 27 '16 at 14:55
  • 1
    @DenysSéguret you put a try without a catch or finally in try-with-resources (although that's not what is here, obviously; just saying that's an example of where you would). – Andy Turner Jan 27 '16 at 15:07
  • @AndyTurner Good observation – Denys Séguret Jan 27 '16 at 15:09
  • @JohnDoe I suspect you are using Java 6 or earlier (since the error message for 7+ is `error: 'try' without 'catch', 'finally' or resource declarations`). You might want to consider upgrading, since Java 6 reached end-of-life nearly 2 years ago. – Andy Turner Jan 27 '16 at 15:15
  • I have reopened the question, since it's not a duplicate of the suggested question. – nhahtdh Jun 20 '16 at 06:00

4 Answers4

1

Because a try without a catch or a finally makes no sense at all. It just does nothing so you'd have the same result if you just omit the try-block.

tkausl
  • 13,686
  • 2
  • 33
  • 50
0

It's required that a try statement has either a catch or finally clause because it would make very little sense in having a try statement without it.

The whole idea with the try statement is that there is some kind of handling to be done if the try-block throws exception. This handling can either be to catch an exception (using catch) or do something after the block has finished or an exception has been thrown (using finally). Then to have no such action associated with the try statement makes the try statement pointless.

skyking
  • 13,817
  • 1
  • 35
  • 57
  • 3
    It would be nice if one explains why one downvotes answers. Is there a problem with the answer? If the problem was explained the answer could be improved. – skyking Jan 27 '16 at 14:49
0

You cannot have a try not followed by a catch or a finally In order to fix it add a catch or a finally or both like this:

try{
    for(int j = 0; j <= i.length; j++)
    {
        System.out.println(i[j]);
    }
}
catch(ArrayIndexOutOfBoundsException e)
{
    System.out.println("Catch");
}//no errors

try{
    for(int j = 0; j <= i.length; j++)
    {
        System.out.println(i[j]);
    }
}//syntax error
catch(Exception e) {
// ... handle errors ...
} finally {
// ... cleanup that will execute whether or not an error occurred ...
}
GroundIns
  • 541
  • 1
  • 5
  • 11
0

finally guarantees the code in finally block always execute no matter there's exceptions or not, and you can write cleancode in finally block except handling exceptions, for example close a file or close a socket in it. try-catch is for a caught exception that you can fix it locally.

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125