30

I'm working with an API that claims to return true if it succeeds, and false if it fails. But, it also claims to throw different exceptions if it fails. How can it return false and throw an exception?

mark
  • 331
  • 1
  • 4
  • 7

4 Answers4

31

It's not possible to both throw an exception and return a value from a single function call.

Perhaps it does something like returning false if there's an error, but throwing an exception if the input is invalid.

edit: PaulPRO posted a (now-deleted) answer pointing out that it is technically possible to cause an exception to be thrown in a different thread, while returning a value in the current one. I thought this was worth noting, even if it's not something you should ever see.

Community
  • 1
  • 1
Jeremy
  • 1
  • 85
  • 340
  • 366
  • Thats what I figured. I thought there might be some trick with a finally block though. – mark Aug 28 '11 at 09:06
  • once an Exception is thrown your program stops running so imho it's impossible to do both. I do wonder if you have a return statement in your 'try' block, does the program still passes the 'final' block, because if you'd return a value in the try block and put a throws Exception in the Final block you would've done both.. – Karel-Jan Misseghers Aug 28 '11 at 09:14
  • 2
    @Karel-Jan Misseghers: no, in Java your program does **DEFINITELY NOT** stop running once an exception is thrown. You're confused as to how Java exceptions do work and obviously haven't done any multi-threaded Java programming yet. You can have a lot of threads happily keep working when an unchecked exception happens in another thread. There are a lot of cases where the dying thread is automagically restarted (the most famous one being Swing's EDT on some OS / JVMs: if the EDT dies, a new EDT is restarted). – SyntaxT3rr0r Aug 28 '11 at 09:58
  • 1
    ahaa, multithreaded, I'll just be quiet now (I have seen the theory of multithreaded programming in Java but haven't used it Yet) sorry for the confusing! – Karel-Jan Misseghers Aug 28 '11 at 11:52
  • 1
    @Karel-Jan: If you have a return in a try-block, it still will execute the finally block (and then return, if the finally-block does not throw an exception). If you have a return in a finally-block, it will suppress the exception (if any) and instead return. (See [chapter 14 of the JLS](http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.17).) – Paŭlo Ebermann Aug 28 '11 at 12:07
27

You can throw an exception that has a (in this case boolean) value:

public class ValueException extends Exception {
    final boolean value;

    public ValueException(boolean value, String message) {
        super(message);
        this.value = value;
    }

    public boolean getValue() {
        return value;   
    }
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

While it is possible to write your code in such a way that an exception AND a value can be derived from a function call (see the above posts), it should NEVER be done in proper coding.

I would love to a see a link to the documentation on this API. API's should place a priority on clarity. Throwing an exception and returning a value leaves the question of whether the value that was returned is safe to use or if it is invalid.

Remember, try/catch blocks are the OTHER method of handling exceptions. They don't pass the exception to the calling method, but handle it internally in a way that the developer deems appropriate.

If, for debugging purposes, you need to see the resulting value in the case of an exception, then Bohemian's idea works well.

HesNotTheStig
  • 549
  • 1
  • 4
  • 9
0

Although this question is old, I found a library that does so. It is to note that this uncommon and might be misleading to add values to the error object. This library wraps that behavior under the hood so you don't need to do it yourself and cause confusion for others working on your code: https://github.com/bacloud23/eager_return_js

It is in JavaScript but the same implementation can be done in Java.

BenGee23
  • 43
  • 6