Return Statement in Try Catch Finally Block in Java
In the last tutorial, we have known about different cases of the control flow of try catch finally block in Java with the help of advanced example programs.
Now, two famous questions arise in the topic “try catch finally block” that
- Can we define return statement in try block or catch block or finally block in Java?
- If we return a value in try block or catch block or finally block, what will happen?
In this tutorial, we will learn all the different cases of try-catch-finally block with return statements.
Return Statement in Try Block only
Case 1: Return statement in try block but do not have a return statement at the end of method
Let’s take an example 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?
Example 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.
Example 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; // return statement at the end of a method.
}
public static void main(String[] args)
{
TryReturnTest2 ft = new TryReturnTest2();
System.out.println(ft.m1());
}
}
Output: I am in try block 50
This is a valid case because we have returned a value 20 at the end of method.
Case 3: Return statement in try block and end of method but statement after return.
Let’s take an example program where we will write a statement after the return statement and see what happens?
Example 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 the preceding program, you will get an unreachable code error. This is because 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.
Example 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());
}
}
Output: I am in try block I am in catch block 20
In the preceding code, an exception occurred in a try block, and the control of execution is transferred to catch block to handle exception thrown by the try block.
Due to an 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 the calling method.
Return Statement in Try-Catch block
Case 5: Return statement in try-catch block.
Let’s take an example program in which we will declare a return statement inside try-catch block.
Example 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());
}
}
Output: I am in try block 50
Case 6: Return statement in try-catch block and a statement at end of method.
Example 6:
package finallyProgram;
public class TryCatchReturn2
{
int m1()
{
try {
System.out.println("I am in try block");
return 50; // return statement inside try block.
}
catch(Exception e)
{
System.out.println("I am in catch block");
return 30; // return statement inside the catch block.
}
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
Example 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 statement inside the catch block.
}
return 100; // return statement at the end of method
}
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.
Example 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
Example 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 and finally blocks
Example 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 and finally blocks but a statement after finally block
Example 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
In this tutorial, we have covered different cases of return statement in try-catch and finally blocks in Java with the help of various example cases. We hope that you will have understood all the basic cases and practiced example programs.
Thanks for reading!!!