Return statement in Try(),catch() and finally() blocks


scenario:1
If method having return type then we can write a return statement either in finally() block or end of the method body, apart from these two places no need to write return statement any where else,it means not required to write return statement in try() and catch() blocks.(optional to write return statement in try() and catch () block)

public static int display(){
	try {
		// some code here
	} catch (Exception e) {
		
		//some code here
	} finally{
		return 1;		
	}
	
 }

                        or

public static int display(){
	try {
		// some code here
	} catch (Exception e) {
		
		//some code here
	} finally{
		//some code here		
	}
	return 1;
 }

scenario:2
If we do not mention return statement either in finally() block or at the end of method body then we should write the return statement in both try () and catch() blocks.other wise it will give compile time error

public static int display(){
	try {

		return 1;

	} catch (Exception e) {

		return 2;

	} finally{
		//some code here		
	}
	
 }

scenario:3
If we mention return statement in finally() block and at the end of method body then it will give compile time error

public static int display(){
	try {

		return 1;

	} catch (Exception e) {

		return 2;

	} finally{
		return 3;
	
	}
	return 4; // unreachable code 
 }

scenario:4
If we mention return statement either in try() block or catch () block then we should write return statement in finally() block or at the end of method body otherwise it will give compile time error


  public static int display(){
	try {
		
		return 1;
	} catch (Exception e) {
		
	} finally{
		
	}
	return 4;
 }
                 or 

 public static int display(){
	try {

	} catch (Exception e) {
		 return 2;
		
	} finally{
		return 3;	
		
	}
	
 }

scenario:5
If we mention return statement in try() block and catch () block then it is optional to write return statement in finally() block , in this scenario if we trying to write return statement at the end of method body it will give compile time error


  public static int display(){
	try {
		
		return 1;
	} catch (Exception e) {
		return 2;
	} finally{
		return 3 // it is optional
	}
	
 }
                 
// Below logic for unreachable code
 public static int display(){
	try {
      return 1;
	} catch (Exception e) {
		 return 2;
		
	} finally{
			
	}
	return 3;	// unreachable code
 }

scenario:6
If we mention return statement in try() block and at the end of the method body ,no other places having return statements then try() block return statement value will print on the console when any exception is not occurred in try() block ,if any exception will occur in the try () block then at the end of the method body return statement will print on the console.


  public static int display(){
	try {
		
		return 1;
	} catch (Exception e) {
		
	} finally{
		
	}
	return 2;
 }
output is :
 if exception not occurred then  output is :1
if exception is occurred then output is : 2

scenario:7
If we mention return statement in catch() block and at the end of the method body ,no other places having return statements then at the end of the method body return statement value will print on the console either exception will occur or not ,it does not matter .


  public static int display(){
	try {
		
		
	} catch (Exception e) {
		return 1;
	} finally{
		
	}
	return 2;
 }
output is : 2

scenario:8
If we mention return statement in try() block, catch() block and finally() block then finally() block return statement value will print on the console either exception will occur or not ,it does not matter .


  public static int display(){
	try {
		return 1;		
	} catch (Exception e) {

		return 2;
	} finally{

		return 3;
	}
	 }
output is : 3