Java Try-Catch-Finally Block with Example

In this chapter, we will learn try-catch and finally block in Java with Example. I hope you have learned Exception Handling in Java in our previous chapter.

Exception Handling is an abnormal condition that may occur in our code at runtime. To avoid such kind of fault we need to handle some exception at the programming level.

For that, Java provides pre-defined keywords such as Try and Catch.

Exception Handling – A Good Practise?

Handling Exceptions are always good because they ensure that your code will work fine in every situation and will not break on runtime.

Always make sure to put your buggy code in Try Block, followed by either Catch or Finally statement. Avoid putting every statement in Try-Catch Block.

Try-Catch Block in Java:

Syntax:

try{    
//code that may throw an exception    
} catch(Exception_class_Name obj){
//handle exception.
}

As you all know try-catch block is used to handle exception in Java. So let’s see a few examples of Java Try-Catch Block:

Example 1: Without Exception Handling

public class Example1 {
	public static void main(String[] args) {
		int data = 10 / 0; // may throw exception
		System.out.println("Value = " + data);
	}
}

Output: Exception in thread “main” java.lang.ArithmeticException: / by zero

Example 2: With Exception Handling

public class Example2 {

	public static void main(String[] args) {
		try {
			int data = 10 / 0; // may throw exception
		}
		// handling the exception
		catch (ArithmeticException e) {
			System.out.println(e);
		}
		System.out.println("Rest Code");
	}
}

Output: java.lang.ArithmeticException: / by zero
Rest Code

Here, you can see an exception is occurred and handled. Also, the rest of the code also worked fine.

Finally Block in Java:

Syntax:

try{    
  //code that may throw an exception    
} catch(Exception_class_Name obj){
  //handle exception.
} finally {
  // this block is executed everytime.
}

Example 1:

public class Example {
	public static void main(String[] args) {

		try {
			int data = 10 / 0; // may throw exception
		} catch (ArithmeticException e) {
			// handling the exception
			e.printStackTrace();
		} finally {
			// this block is executed everytime.
			System.out.println("finally block executed");
		}

		// rest code will be executed
		System.out.println("Outside try-catch-finally");
	}
}

Output:

java.lang.ArithmeticException: / by zero at Example.main(Example.java:7)
finally block executed
Outside try-catch-finally

Internal Working of Java Try-Catch Block:

If the exception is not handled in Java program then JVM will provide a default exception and internally:

  • Prints out exception.
  • Prints the stack trace.
  • Terminate the program.

Remember:

  • Always use Try-Catch where an exception may occur.
  • Try Block always comes with Catch Block.
  • Finally is executed every time.
  • If an exception occurs in Catch block then JVM will throw an exception if not handled again.

I hope you’ve learned try-catch-finally in this tutorial.

Happy Learning!