gudii9
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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("Divisi on 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.");
}
}
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("Divisi
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.");
}
}
ASKER
my question is more with example method which expects return not void.
i am trying to find example to explain more clearly
i am trying to find example to explain more clearly
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
try{
//Do blah
return "success"; //Assuming everything goes well, return success.
}catch (Exception e){
e.printStackTrace();
}
return "error";
}
like above
ASKER
this link is somewhat similar to my challenge but not completely
http://codereview.stackexchange.com/questions/28570/try-catch-block-inside-a-method-that-returns-string
http://codereview.stackexchange.com/questions/28570/try-catch-block-inside-a-method-that-returns-string
ASKER
please advise
ASKER