WHY WE WRITE THE CLEAN UP CODE IN finally BLOCK?

All the clean up code is written inside the finally block because there are more than one catch clauses so each each then requires its own copy of the cleanup code.
for example,
you allocated a network socket or opened a file somewhere in the guarded region,
each exception handler would have to close the file or release the socket. That
would make it too easy to forget to do cleanup, and also lead to a lot of redundant
code. To address this problem, Java offers the finally block.

No comments:

Post a Comment