Java Try Keyword

Java Try Keyword


Try Keyword In Java
Java exception handling is managed via five keywords: try, catch, throw, throws, and finally.
Briefly, here is how they work. Program statements that you want to monitor for exceptions are contained within a try block.
If the error come in the program it is handled by try keyword and the catch by catch keyword
As we know try is  a reserve word hence it will always be written in small case.
Now we will see the syntax, and example of try-catch keyword.
 
Syntax
 
This is the general form of an exception-handling block-
 
try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// ...
finally {
// block of code to be executed after try block ends
}
 
Example -  ArithmeticException generated by the division-by-zero error
 
 
Output -
 
 
We saw the exception occurrence in the previous example now we will deal with this with the help of an exception handling
 
Example-# ArithmeticException generated by the division-by-zero error using try-catch -
 
 
Output -
 
 
Example -
 
 
Output -