Explain abt try-catch-finally ?what is the exact use?

Showing Answers 1 - 8 of 8 Answers

prabhakar

  • Feb 28th, 2006
 

try-catch-finally comes handy when dealing with exceptions.this is useful in telling the jvm what it has to do when an exception is encountered.we have to insert all the code which is suspicious about causing an exception inside the try block.so when the specified exception is encountered the statements inside catch block is executed.finally is used to execute the code inside it under any cicumstances even if the exception is thrown.

  Was this answer useful?  Yes

getmahi

  • Mar 14th, 2006
 

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

  Was this answer useful?  Yes

Vickey

  • Nov 28th, 2006
 

In java ... we often get free errors ... Somtimes ... the error might be in  syntax those errors as know as Complication errors ... and the seconf type of errors is Runtime errors ...i.e.. the program may run succesfully but may not provide the possible desired output ... for example division by zero .. it is not possible so ...these kind of errors sometimes lead to crash of the system ..

So java  has come up with a concept of keywords Try , Catch , Finall y ...

In Try block we have put the code the Code that might generate an error ...

and int Catch block we must put the code for the error generated and caught by the Try block ... its not but Exception Handling ...

and the last come Finally  ... It executes regardless of whether the error encountered or not ...Its similar to the Default case in the Switch ... Case ... Construct we have ...

 

  Was this answer useful?  Yes

drjava72

  • Mar 16th, 2010
 

Try Catch are necessary for checked exceptions and you can use them for unchecked exceptions.Suspecious code is written in try and exception is handled in catch block and the finally block will execute every time weather exception is raised or not.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions