Answers

Question and Answer:

  Home  Java Classes

⟩ Can you explain try-catch-finally?What is the exact use?

try - catch and finally are used for avoiding situations when a program may get terminated due to the occurance of unwanted error during the exection of the program.

The following points are important....

1)There is only one try for a block......any number of catch statements for a block and only one finally for a block of statments

2)finally is optional.

3) catch is also optional but ,if the catch statement is missing then finally must appear.

4) All catches corresponding to the child exceptions must be appearing before a catch for a parent exception..

5) Irrespective of the occurance of the exception ,the statements present in the finally block are executed always with one exclusion.

i.e. IF a System.out.exit() statement is encountered then the program terminates imediately,,,hence finally can not be executed in such cases.

Note: even there is an return statement appears in try block ...then also the code in finally is executed.

e.g. Guess what happens.....if ur try has return 1; and finally has return 2; ...? 2 is returned

 132 views

More Questions for you: