32

So when I do a code of blocks inside a try{}, and I try to return a value, it tells me

no return values

import org.w3c.dom.ranges.RangeException;


public class Pg257E5 
{
public static void main(String[]args)
{
    try
    {
        System.out.println(add(args));
    }
    catch(RangeException e)
    {
        e.printStackTrace();
    }
    finally
    {
        System.out.println("Thanks for using the program kiddo!");
    }
}
public static double add(String[] values)
// shows a commpile error here that I don't have a return value
{
    try
    {
        int length = values.length;
        double arrayValues[] = new double[length];
        double sum = 0;
        for(int i = 0; i<length; i++)
        {
            arrayValues[i] = Double.parseDouble(values[i]);
            sum += arrayValues[i];
        }
        return sum; // I do have a return value here.
        // Is it because if the an exception occurs the codes in try stops and doesn't get to the return value?
    }
    catch(NumberFormatException e)
    {
        e.printStackTrace();
    }
    catch(RangeException e)
    {
        throw e;
    }
    finally
    {
        System.out.println("Thank you for using the program!");
        //so would I need to put a return value of type double here?
    }

}
}

My question is, how do you return a value when you are using try and catch?

1
  • 2
    You don't need the RangeException catch block. Jul 1, 2013 at 13:37

4 Answers 4

43

To return a value when using try/catch you can use a temporary variable, e.g.

public static double add(String[] values) {
    double sum = 0.0;
    try {
        int length = values.length;
        double arrayValues[] = new double[length];
        for(int i = 0; i < length; i++) {
            arrayValues[i] = Double.parseDouble(values[i]);
            sum += arrayValues[i];
        }
    } catch(NumberFormatException e) {
        e.printStackTrace();
    } catch(RangeException e) {
        throw e;
    } finally {
        System.out.println("Thank you for using the program!");
    }
    return sum;
}

Else you need to have a return in every execution path (try block or catch block) that has no throw.

4
  • 2
    What if I do not want a valid value to be returned? Is it a bad practice to return null? Thanks for any advice.
    – Jeff Hu
    Aug 2, 2017 at 2:41
  • 3
    @JeffHu: Returning null is not always the best solution. In Java throwing an Exception is more often the appropriate way to handle exceptional conditions.
    – Uwe Plonus
    Aug 16, 2017 at 15:03
  • I would return in finally block, so it happens no matter what. Jul 12, 2018 at 18:23
  • @JeffHu Uwe is quite right, though I would add that throwing exceptions is meant for situations in which the application execution has become unrecoverable. I'd argue it's oftentimes better to return 'result' objects, that informs the caller of the outcome of an operation - herein included the result itself or an error message. At any rate returning null should be avoided, if possible/practical. Aug 7, 2018 at 11:54
4

Here is another example that return's a boolean value using try/catch.

private boolean doSomeThing(int index){
    try {
        if(index%2==0) 
            return true; 
    } catch (Exception e) {
        System.out.println(e.getMessage()); 
    }finally {
        System.out.println("Finally!!! ;) ");
    }
    return false; 
}
3

It is because you are in a try statement. Since there could be an error, sum might not get initialized, so put your return statement in the finally block, that way it will for sure be returned.

Make sure that you initialize sum outside the try/catch/finally so that it is in scope.

2

The problem is what happens when you get NumberFormatexception thrown? You print it and return nothing.

Note: You don't need to catch and throw an Exception back. Usually it is done to wrap it or print stack trace and ignore for example.

catch(RangeException e) {
     throw e;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.