Can we write return statement in try catch or finally block.suposse we write return 0 in try catch or finally. can we do.

Showing Answers 1 - 24 of 24 Answers

Atanu Shankar Bagchi

  • May 26th, 2006
 

Yes we can.

  Was this answer useful?  Yes

samiha

  • May 26th, 2006
 

According to me..we can write return method in try catch block..it will work..

  Was this answer useful?  Yes

Dipthi

  • May 27th, 2006
 

Yes,We can write the return statement in try catch & finally. The only thing is that even if we write return statement in try bloack or catch block the finally block will always get executed.The only case in which finally block does not execute is when we write system.exit(0) in either try or catch.

  Was this answer useful?  Yes

Srikanth

  • May 27th, 2006
 

class Srikanth
{

 public static void main(String args[])
 {

  Srikanth s=new Srikanth();
  int sri=get();

  System.out.println("return value"+sri);

 }

 public int get()
 {
  try
    {

    int a=0;
    int b=5;
    int c=b/a;

    System.out.println("c....."+c);

    return 0;
    }
    catch(Exception e)
    {

    System.out.println("Exception....."+e);
    return 1;
    }

    finally
    {
     System.out.println("Finally Block.........");
     return 0;
    }
  }
}

According to this program v can return a value from try/catch.finally block

Amitkumar

  • May 30th, 2006
 

Ofcourse we can write return statement in all three (try, catch, finally) blocks.

Depending on the flow of exception either the return statement from try block or the return statement in catch block will get executed. But as we also have return in finally the return value of finally will override the previously return value either from try or catch block.

We can write return in try, catch or finally, however in finally block even if return is the last statement it would give warning while compilation saying “finally block does not complete normally”. It would be ideal to set a variable eg. Int returnType, and assign diff values in try, catch, finally and specify return after the finally block completes.

  Was this answer useful?  Yes

Naveen

  • Sep 7th, 2011
 

Yes finally block accepts return statement ..

its executes every time either we have return statement in try , or in catch block..

but its doesn't executes if try, catch blocks have System.exit(0); statement..

Code
  1. class FinalRetun

  2. {

  3.  

  4.  public static void main(String args[])

  5.  {

  6.  

  7.   FinalRetun fr=new FinalRetun();

  8.  

  9.           int ret=fr.returnValue();

  10.  

  11.   System.out.println("return value"+ret);

  12.  

  13.  }

  14.  

  15.  public int returnValue()

  16.  {

  17.   try

  18.     {

  19.  

  20.     int a=0;

  21.     int b=5;

  22.     int c=b/a;

  23.  

  24.     System.out.println("c  value "+c);

  25.     System.exit(0);

  26.  

  27.     return c;

  28.     }

  29.     catch(Exception e)

  30.     {

  31.  

  32.     System.out.println("Exception caught as ....."+e);

  33.     return 1;

  34.     }

  35.  

  36.     finally

  37.     {

  38.      System.out.println("Finally Block.........");

  39.      // System.exit(0);

  40.  

  41.      return 2;

  42.     }

  43.   }

  44. }

  Was this answer useful?  Yes

just check out this program
try{
return 1;
}
catch(Exception e){
return 2;
}
finally{
return 3;
}


it will return 3 .. even in the error conditions also......

  Was this answer useful?  Yes

satish

  • Sep 25th, 2015
 

Yes we can.

  Was this answer useful?  Yes

saurabh

  • Sep 1st, 2016
 

if we write return statement in try block
will then finally block be executed?
try{
return ture;
}
catch()
{
}
finally{
??
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions