Is it necessary to use catch block while using try block? Can anybody explain me about it in detail..

Questions by parimicnu

Showing Answers 1 - 7 of 7 Answers

sudhakar

  • Apr 7th, 2006
 

yes it is necessary coz after trying to find the exception somewhere in the code it should be caught in order to know what exception has occured.So catch block definitely follows try block.Onemore is even for multiple try blocks we may have only one catch block.

  Was this answer useful?  Yes

Its is not necessary that every try should have a catch block. catch block basically catches the Exception that is raised in the try block by not letting it go to the JVM, if we do not have a catch the Exception is absorbed by the JVM and it aborts the program from where it has encountered the Exception.

  Every try must have either catch or finally block or both.

 Finally block which is executed irrespective of whether catch exists or not.

  Was this answer useful?  Yes

vinayak

  • Apr 8th, 2006
 

it is not necessary to have a catch block we can use finally block instead of catch

cu bye

all the best

from vinayak

  Was this answer useful?  Yes

Yes its necessary we need to give atleast one catch block or finally block.

TRY-Catch-Finally:

try{}

catch(IOException e){}

catch(FileNotFoundException e){}

catch(Exception e){}

finally{}

Function of the try block is to identify a block of statements within which an exception might be thrown

Function of the catch block is to identify if any exception is thrown from try block and that exception is caught by the corresponding catch block and that catch block ststements will get excecute and catch blocks will not excecute.

Function of the Finally block is to identify regardless of any exception occur in try block or not, this block will get excecuted.

Conclusion:

so if try block has got any exception it will search for other blocks to hand over that exception. so we need to give atleast one exception without finally block. or finally block without any catch blocks.

so a try block without any catch blocks or without finally block is a compile time error

  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