0

What is the need of finally block when we can close files and all in catch block. Whatever thing we are closing or clearing in finally block can it be done in catch block. Please tell me if I am wrong.

Erba Aitbayev
  • 4,167
  • 12
  • 46
  • 81
pradeep
  • 31
  • 7

2 Answers2

6

You normally want to perform the same clean-up action whether an exception is thrown or not. Doing that just with catch blocks is painful - especially if you want to make sure that you only call close() once, even if that throws. You'd end up with:

bool closeCalled = false;
OutputStream stream = ...;
try {
   stream.write(...);
   closeCalled = true;
   stream.close();
} catch (IOException e) {
   if (!closeCalled) {
       // TODO: Add another try/catch here? What do we want to
       // do if this throws?
       stream.close();
   }
   throw e;
}

Compare that with:

OutputStream = ...;
try {
   stream.write(...);
} finally {
   // TODO: Still need to work out what to do if this throws.
   stream.close();
}

Or best:

try (OutputStream stream = ...) {
    stream.write(...);
}

Personally, I think the last example is by far the cleanest - do you really want the first block of code everywhere? Oh, and this is a simple case - we're only catching a single exception. Imagine repeating that code in each catch clause, and if there are multiple ways of exiting the try block, repeating the close call for each of those, too... ick.

Additionally, as pointed out by Nicolas in comments, there are all those unchecked exceptions which you don't catch (and which can be painful to catch and rethrow). Fundamentally, the principle of "I just want to clean up my resources whatever happens" is very, very compelling...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @NicolasFilotto: That's true, you'd have to catch `Throwable` everywhere and rethrow in nasty ways. Will add that in. Although even if that *weren't* a problem, I'd still want try/finally... – Jon Skeet Sep 23 '16 at 14:59
  • Thanks Jon for your help – pradeep Sep 27 '16 at 14:25
  • Even if one doesn't want to perform *exactly* the same cleanup actions in exception/non-exception cases, being able to perform the common parts in a `finally` block is helpful; the try-with-resources will even take care of some aspects of that automatically (e.g. if the main-line code succeeds, but cleanup throws an exception, it will propagate the exception from the cleanup; if the mainline throws an exception and the cleanup does too, try-with-resources will propagate the mainline's exception while attaching the suppressed exception from the cleanup. – supercat Oct 04 '16 at 22:08
0

what is the need of finally block when we can close files and all in catch block. whatever thing we are closing or clearing in the finally block can it be done inn catch block

catch block will be executed only when an Exception arises in the try block.

When you want to do some operations every time like Clean up , You need to write the code in finally block.

Finally will be executed always and catch block will not executed always.

Consider the below example ,

you opened a file and try to copy to another file in the try block, If code executes fine , catch block will not be executed and you ending on not closing the file

Venkat
  • 2,549
  • 2
  • 28
  • 61