try and catch keywords in Java: Syntax and Examples

try keyword is used to monitor a code for checking exception handling in Java. catch keyword is used to catch the exception found in the try block. Once an exception is caught in catch block the return never goes back to try block. try and catch work together. However, you can use multiple catch statements for one try statement. Also, try and catch can’t be used for single statements; they must be used for code blocks.


try {


catch (ExceptionType exOb) {


For Example:

class Test {

public static void main(String args[ ]) {

int a, b;

try {

a = 0;

b = 5 / a;

System.out.println(“This line is skipped.”);

} catch (ArithmeticException e) {

System.out.println(“Divide by zero.”);


System.out.println(“catch statement ends.”);




Divide by zero.

catch statement ends.

Leave a Reply