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
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
please post your code.
0
ozoCommented:
Not necessarily.  Why do you ask?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
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
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

gudii9Author Commented:
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.");
}
}
0
gudii9Author Commented:
my question is more with example method which expects return not void.

i am trying to find example to explain more clearly
0
gudii9Author Commented:
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
0
gudii9Author Commented:
0
gudii9Author Commented:
please advise
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.