Senin, 14 Maret 2016

Return statement in try catch block java

1.Can we write return statement in try or catch blocks in java

  • Inside method if we have some statements which may proven to raise exceptions we need to keep those statements in side try catch blocks in order to handle the exceptions.

  • There is a situation where a method will have a return type and we can return some value at any part of the method based on the conditions.
  • So mixing these two scenarios we may have a situation to return some value from try block or catch block in such cases we need to follow some basic rules.
  • This is also one of the famous interview question  "can we write return statement in try block ", "can we write return statement in catch block ", "what will happen if we return some value in try block or catch block ", "will catch block execute after return statement " and "what are the basic rules we need to follow when we are returning or our method returning some value in try or catch blocks"
  • We will see the all the scenarios and conclusion.
Return statement in try block:

i) return statement in try block only
  • If we return a value in try block and if we do not return a value at the end of the method it leads to compile time exception 
  • Error: This method must return a result of type int

  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){ // Error:This method must return a result of type int
  6.         
  7. try {
  8.  
  9. return 1;
  10.  
  11. } catch (Exception e) {
  12.  
  13. }
  14.         
  15.  System.out.println("End of the method");
  16. }
  17.     
  18.     
  19. public static void main(String[] args) {
  20.         
  21.         TryCatchReturn obj = new TryCatchReturn();
  22.        
  23.  
  24. }

  25. }
     

ii) return statement in try block and end of the method but after return one statement

  • Even though it in this condition it wont reach end of the method still it asks us for return at end of the method because in some cases there may be a chance of getting exception in try block so it will not completely execute try in this case we don not have return in catch so at end of the method it expects some value to be return because the  method have some return type.
  • If we are keeping return statement in try block only there may be a situation of chance of raising exception and try will not execute completely and it goes to catch block that is the reason it expecting a return at catch or end of the method 
  • So in this scenario we will try to keep return but after return we have written one statement

  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){
  6.         
  7. try {
  8.  
  9. return 1;
  10.  
  11. } catch (Exception e) {
  12.  
  13. }
  14.  
  15. return 10; 
  16.  System.out.println("End of the method"); // Error : Unreachable code
  17. }
  18.     
  19.     
  20. public static void main(String[] args) {
  21.         
  22.         TryCatchReturn obj = new TryCatchReturn();
  23.        
  24.  
  25. }

  26. }
     

  • in the above program last statement will not execute at any condition so it became unreachable code as we know java does not supports unreachable codes so it will raise compile time error.
iii) return statement in try block and end of the method

  • This is the correct and successful scenario with respect to try with return statement in java
  • Lest see a java program on this




  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){
  6.         
  7. try {
  8.  
  9. return 1;
  10.  
  11. } catch (Exception e) {
  12.  
  13. }
  14.  
  15. System.out.println("End of the method");
  16. return 10; 
  17. }
  18.     
  19.     
  20. public static void main(String[] args) {
  21.         
  22.   
  23. TryCatchReturn obj = new TryCatchReturn();
  24.        
  25. System.out.println(obj.calc());
  26.  
  27. }

  28. }
     

Output:

  1. 1

Return statement in catch block:

i) return statement in catch block only
  • If we return a value in catch block and if we do not return a value at the end of the method it leads to compile time exception 
  • Error: This method must return a result of type int

  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){ // Error:This method must return a result of type int
  6.         
  7. try {
  8.  

  9.  
  10. } catch (Exception e) {
  11.  return 1;
  12. }
  13.         
  14.  System.out.println("End of the method");
  15. }
  16.     
  17.     
  18. public static void main(String[] args) {
  19.         
  20.         TryCatchReturn obj = new TryCatchReturn();
  21.        
  22.  
  23. }

  24. }
     

ii) return statement in catch block and end of the method but after return one statement

  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){
  6.         
  7. try {
  8.  
  9.  
  10. } catch (Exception e) {
  11.  
  12. return 1;
  13. }
  14.  
  15. return 10; 
  16.  System.out.println("End of the method"); // Error : Unreachable code
  17. }
  18.     
  19.     
  20. public static void main(String[] args) {
  21.         
  22.         TryCatchReturn obj = new TryCatchReturn();
  23.        
  24.  
  25. }

  26. }
     

  • in the above program last statement will not execute at any condition so it became unreachable code as we know java does not supports unreachable codes so it will raise compile time error.
ii) return statement in catch block and end of the method





  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){
  6.         
  7. try {
  8.  
  9.  int x=12/0;
  10. } catch (Exception e) {
  11.  
  12. return 1;
  13. }
  14.  
  15. return 10; 
  16.  System.out.println("End of the method"); // Error : Unreachable code
  17. }
  18.     
  19.     
  20. public static void main(String[] args) {
  21.         
  22. TryCatchReturn obj = new TryCatchReturn();
  23.        
  24.  System.out.println(obj.calc());
  25.  
  26. }

  27. }
     
Output:

  1. 1


Return statement with try catch block

try catch with return statement in java


try catch block with return statement
 

Bagikan

Jangan lewatkan

Return statement in try catch block java
4/ 5
Oleh

Subscribe via email

Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.