Return statement in try catch finally block in Java

In the last tutorial, we learned different cases of the control flow of try catch finally block in Java with example programs. Now, two famous questions arise in the topic “try catch finally block” that 

1. Can we define return statement in try block or catch block or finally block in Java?
2. If we return a value in try block or catch block or finally block, what will happen?

In this tutorial, we will learn all different cases of try-catch-finally block with return statements.

Return statement in try block only


Return statement in Java try catch finally block

Case 1: Return statement in try block but do not have return statement at the end of method

Let’s create a program where we will return a value in the try block but will not return a value at the end of method and see what error comes?

Program source code 1:

package finallyProgram;
public class TryReturnTest1 
{
 int m1(){ // Compile time error.
 try {
	 System.out.println("I am in try block");
     return 50;
  }
catch(Exception e)
{
  System.out.println("I am in catch block");
}
 // Here, no return statement at the end of method.
}
 public static void main(String[] args) 
 {
  TryReturnTest1 ft = new TryReturnTest1();
   System.out.println(ft.m1());
  }
}

In the preceding program, we did not return a value at the end of the method. Therefore, we will get compile-time error: “This method must return a result of type int”. So, this is an invalid case.

Case 2: Return statement in try block and end of method.


Let’s take an example program where we will return a value in the try block as well as at the end of method.

Program source code 2:

package finallyProgram;
public class TryReturnTest2 
{
int m1()
{
try {
   System.out.println("I am in try block");
   return 50;
  }
catch(Exception e)
{
  System.out.println("I am in catch block");
 }
 return 20;
}
public static void main(String[] args) 
{
  TryReturnTest2 ft = new TryReturnTest2();
  System.out.println(ft.m1());
  }
}
I am in try block
50

This is a valid case because we have returned value 20 at the end of method.

Case 3: Return statement in try block and end of method but statement after return 

Let’s make a program where we will write a statement after return statement and see what happens?

Program source code 4:

package finallyProgram;
public class TryReturnTest3 
{
 int m1(){
 try {
	 System.out.println("I am in try block");
     return 50;
  }
catch(Exception e)
{
	System.out.println("I am in catch block");
 }
 return 20;
 System.out.println("Statement after return"); // Unreachable code.
}
 public static void main(String[] args) 
 {
  TryReturnTest3 ft = new TryReturnTest3();
   System.out.println(ft.m1());
  }
}

When you will try to execute preceding program, you will get unreachable code error.

Key point: Any statement after return statement will result in compile-time error stating “Unreachable code”.

Case 4: Return statement in try block and at end of method but exception occurred in try block.


Let’s see the output of a program where an exception will occur in try block but returning a value in try block and at the end of method.

Program source code 4:

package finallyProgram;
public class TryReturnTest4
{
int m1()
{
try {
   System.out.println("I am in try block");
   int x = 10/0;
   return 50;
  }
catch(ArithmeticException ae)
{
   System.out.println("I am in catch block");
 }
return 20;
}
public static void main(String[] args) 
 {
  TryReturnTest4 ft = new TryReturnTest4();
   System.out.println(ft.m1());
  }
}
I am in try block
I am in catch block
20

In the preceding code, an exception occurred in try block, and the control of execution is transferred to catch block to handle exception thrown by try block. Due to exception occurred in try block, return statement in try block did not execute. Return statement defined at the end of method has returned a value 20 to calling method.

Return statement in Try-Catch block


Case 5: Return statement in try-catch block

Program source code 5:

package finallyProgram;
public class TryCatchReturn1
{
 int m1()
 {
 try {
	 System.out.println("I am in try block");
     return 50;
  }
catch(Exception e)
{
	System.out.println("I am in catch block");
	return 30;
 }
}
 public static void main(String[] args) 
 {
  TryCatchReturn1 obj = new TryCatchReturn1();
   System.out.println(obj.m1());
  }
}
I am in try block
50

Case 6: Return statement in try-catch block and a statement at end of method

Program source code 6: 

package finallyProgram;
public class TryCatchReturn2
{
 int m1()
 {
 try {
	 System.out.println("I am in try block");
     return 50;
  }
catch(Exception e)
{
	System.out.println("I am in catch block");
	return 30;
 }
System.out.println("Method at end"); // Unreachable code. So, compile time error will occur.
}
 public static void main(String[] args) 
 {
  TryCatchReturn2 obj = new TryCatchReturn2();
   System.out.println(obj.m1());
  }
}

Case 7: Return statement in catch block but no exception in try block

Program source code 7:

package finallyProgram;
public class CatchReturn1
{
 int m1()
 {
 try {
	 System.out.println("I am in try block");
  }
catch(Exception e)
{
	System.out.println("I am in catch block");
	return 30;
 }
return 100;

}
 public static void main(String[] args) 
 {
 CatchReturn1 obj = new CatchReturn1();
   System.out.println(obj.m1());
  }
}
Output:
      I am in try block
      100

Case 8: Return statement in catch block but exception occurred in try block

Program source code 8:

package finallyProgram;
public class CatchReturn2
{
int m1()
{
try {
   System.out.println("I am in try block");
   int x = 20/0;
   System.out.println("Result: " +x);
 }
catch(ArithmeticException ae)
{
  System.out.println("I am in catch block");
  return 30;
 }
return 100;
}
public static void main(String[] args) 
{
 CatchReturn2 obj = new CatchReturn2();
 System.out.println(obj.m1());
  }
}
Output:
      I am in try block
      I am in catch block
      30

Return statement in try catch finally block


Case 9: Return statement in try block and finally block

Program source code 9:

package finallyProgram;
public class FinallyReturn1
{
 int m1()
 {
 try {
   System.out.println("I am in try block");
   return 30;
  }
finally {
  System.out.println("I am in finally block");	
  return 50;
  }
}
public static void main(String[] args) 
{
 FinallyReturn1 obj = new FinallyReturn1();
   System.out.println(obj.m1());
  }
}
Output:
      I am in try block
      I am in finally block
      50

In the preceding code, finally block overrides the value returned by try block. Therefore, this would return value 50 because the value returned by try has been overridden by finally block.

Case 10: Return statement in catch block and finally block

Program source code 10:

package finallyProgram;
public class FinallyReturn2
{
@SuppressWarnings("finally")
int m1()
{
try {
   System.out.println("I am in try block");
   int x = 10/0;
   System.out.println("Result: " +x);
  }
catch(ArithmeticException ae)
{
  System.out.println("I am in catch block");
  return 40;
 }
finally {
  System.out.println("I am in finally block");	
  return 50;
  }
}
public static void main(String[] args)
{
FinallyReturn2 obj = new FinallyReturn2();
System.out.println(obj.m1());
  }
}
Output: 
       I am in try block
       I am in catch block
       I am in finally block
       50

In the above example program, finally block overrides the value returned by catch block. Therefore, the returned value is 50.

Case 11: Return statement in catch block and finally block but a statement after finally block 

Program source code 11:

package finallyProgram;
public class FinallyReturn3
{
@SuppressWarnings("finally")
int m1()
{ 
int a = 20, b = 0;	
try {
   System.out.println("I am in try block");
   int c = a/b;
   System.out.println("Result: " +c);
  }
catch(ArithmeticException ae)
{
  System.out.println("I am in catch block");
  return 40;
 }
finally {
  System.out.println("I am in finally block");	
  return 50;
  }
System.out.println("Statement after finally block");
}
 public static void main(String[] args) 
 {
 FinallyReturn3 obj = new FinallyReturn3();
   System.out.println(obj.m1());
  }
}
Output:
      Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
      Unreachable code

Final words
Hope that this tutorial has covered different cases of return statement in try-catch and finally block in Java with example program. I hope that you will have understood this topic and enjoyed it.
Thanks for reading!!!

Next ⇒ When Finally block is not executed in Java⇐ PrevNext ⇒