try Java Keyword

The try is a keyword defined in the java programming language.

try Java Keyword

try Java Keyword

     

The try is a keyword defined in the java programming language. Keywords are basically reserved words which have specific meaning relevant to a compiler in java programming language likewise the try keyword indicates the following :

-- The try keyword in java programming language is used to wrap the code in a block of statements where there are chances to get some erroneous condition in form of  an exception object.
-- Every try block in java programming language must be followed by a one catch or finally clause at least.
-- In exception handling mechanism,
if an exception class is not handled by any catch block, then that exception propagates up the call stack till it catches the exception else it prints an appropriate error message.

Example to use the try blocks: 

try{
<block of code that may throw exceptions>
}
catch (<java.lang.Exception or subclass> e){
<code_to_handle_exception e>
}

 

try{
<block that may throw different exceptions>
}

catch (Exception1 e){
<code to handle Exception1 e>
}
catch (Exception2 e){
<code to handle Exception2 e>
}

 

try{
<block that may throw exceptions>
}

catch (<java.lang.Exception or subclass> e){
<code to handle exception e>
}

finally{
<statements needed to execute>
}