2

I'm using the code below in a Triangle class to allow the user to set the first, second, or third point of a declared Triangle.

public Point get(String p) throws IllegalArgumentException {
    IllegalArgumentException e = new IllegalArgumentException();
    try {
        if (p == "first") { return first; }
        else if (p == "second") { return second; }
        else if (p == "third") { return third; }
        else { throw e; }
    }
    catch (e) {
        System.out.println("Error: " + e);
    }
}

The compiler is telling me:

Triangle.java:41: error: missing return statement
    }
    ^

But I thought the point of the catch statement was to be able to catch an error and return a string describing the error, without having to worry about matching the function's return type.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
UnworthyToast
  • 825
  • 5
  • 11
  • 21
  • You're [comparing strings by reference](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) but that particular error is because the `catch` block doesn't lead to a `return` or a `throw` and this method needs to return a `Point`. – Mike Samuel Nov 05 '15 at 03:41
  • Please don't answer duplicates. See [Why is Stack Overflow so negative of late?](http://meta.stackoverflow.com/a/252077) – Basilevs Nov 05 '15 at 03:54

4 Answers4

6

Because you're missing a return statement.

The method declares that it returns something, so it must return something (or throw an exception). The compiler can't guarantee that any of the return statements in the try block will be reached if an exception is thrown before any of them execute. So the catch block also needs to return something (or throw an exception, or return something after the try/catch construct entirely).

Edit: Looking again, you're also potentially missing a return in the try block. (If you don't have one after the entire try/catch structure.) What if none of the conditions in the if/else structure are satisfied? Nothing is returned. Which is invalid.

Basically, all logical paths must result in a valid exit of the method. You've missed two such paths.

David
  • 208,112
  • 36
  • 198
  • 279
2

You're not returning anything in your function on several paths.

But I thought the point of the catch statement was to be able to catch an error and return a string describing the error, without having to worry about matching the function's return type.

That's not at all what a try-catch does, and moreover your function is declared to return a Point not a String.

try-catch simply "catches" a Throwable (Error or Exception) and allows you to run some code when it is thrown instead of simply terminating the application with an Uncaught Exception/Error.

You need to return some value from your function after the try-catch there is no way to return a string, nor is there a language construct in place that behaves like you've explained your understanding of try-catch.

Also your code cna't actually throw an IllegalArgumentException so your catch block will never get called. In this case, it sounds like what you want is instead something like this

public Point get(String p) throws IllegalArgumentException {
        if (p == null) { throw new IllegalArgumentException(); }
        if (p.equals("first")) { return first; }
        else if (p.equals("second")) { return second; }
        else if (p.equals("third")) { return third; }
        else { throw new IllegalArgumentException(); }
}

The code could then be called like so

Point p;
try {
    p = get("notFirst");
} catch (IllegalArgumentException ex) {
    //oh no, we gave a bad argument, and the method told us nicely.
}
CollinD
  • 7,304
  • 2
  • 22
  • 45
  • I don't see him returning a string, I see him returning a named variable of an unknown type, which may very well be a Point – Shadow Nov 05 '15 at 03:45
  • 1
    I agree @Shadow but read the quoted section please. "return a string describing the error without having to worry about matching the function's return type" It sounds a lot more like he wants to `throw` an `Exception` rather than handle one (since none of that code can actually throw an `IllegalArgumentException` anyway. – CollinD Nov 05 '15 at 03:46
  • Yeah but by "return a string" he is referring to using `System.out.println()` in his `catch` block. Either way +1 for the `throws IllegalArgumentException` on the function – Shadow Nov 05 '15 at 03:50
  • Ah that would make sense. Well I guess I'll leave this up as is and let him come back with clarifications if possible. Thanks Shadow. – CollinD Nov 05 '15 at 03:51
1

You are missing two parts: 1. A return statement in try block for else condition 2. Catch block doesn't lead to a return statement or a throw statement

I don't know if type of first, second, third variables are string or Point, but you should return with Point, because your function is : public Point get(String p) { ... }

-1

You have three if statements. What happens when the input doesn't satisfy any of those? Your method doesn't have a return statement for that case.

Eric Guan
  • 15,474
  • 8
  • 50
  • 61