Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

method return

Hi,

i have a method with try catch.


Do i need to have separate return within try block and also one other return after catch block( i see null is being reurned there which is not clear why to me).

Please advise
SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

my question is can catch block itself return null as return as soon as it comes out of if block and comes inside catch block
Avatar of gudii9

ASKER

class Exc2 {
public static void main(String args[]) {
int d, a;
try { // monitor a block of code.
d = 0;
a = 42 / d;
System.out.println("This will not be printed.");
} catch (ArithmeticException e) { // catch divide-by-zero
//error
System.out.println("Division by zero.");
return null;//can i do this??? any way flow did not go through 'if' loop then it can only go to 'else' loop right? is there is scenario flow does not go through 'if' or 'try'
}

return null;//is this is needed???
System.out.println("After catch statement.");
}
}
Avatar of gudii9

ASKER

my question is more with example method which expects return not void.

i am trying to find example to explain more clearly
Avatar of gudii9

ASKER

public String execute() throws Exception{
    try{
        //Do blah
        return "success"; //Assuming everything goes well, return success.
    }catch (Exception e){
        e.printStackTrace();
    }
    return "error";
}

like above
Avatar of gudii9

ASKER

please advise