3

Exceptions are wonderful things, but I sometimes worry that I throw too many. Consider this example:

Class User {

 public function User(user){ // Query database for user data if(!user) throw new ExistenceException('User not found'); } 

}

I'd argue that it makes as much sense to simply return false (or set all user data to false in this case), rather than throwing an exception.

Which do you prefer?

2
  • 1
    I got the impression, from reading "Learning Python" that at least that language is really 'exception friendly': the authors recommended using exceptions for some sorts of control flow (and indeed, many 'errors' like EOF are raised exceptions). So, does it depend on the language? Commented Mar 31, 2010 at 18:09
  • @Adriano: Well-observed. To some extent, I think it does. Commented Mar 31, 2010 at 18:13

4 Answers 4

4

Throw exception in exceptional circumstances.

If the condition is something you expect in the normal run of the program, check for it and report an error. Reserve exceptions for things that should never happen, or as an indication to other programmers that something unrecoverable has occurred.

In your case, returning false makes more sense (or perhaps use a null object as a return value).

Sign up to request clarification or add additional context in comments.

2 Comments

Looks like there is some opposite answers on the subject on SO, how would you do if you need to get the cause of the failure (login not found / password incorrect / ...)? Using error codes is discouraged: stackoverflow.com/questions/4044995/… so you're back to exception handling and it is not for exceptional circumstances?
@Guillaume86 - Like many other things in programming, it's about the balance. I would argue that if you expect errors to happen, then you shouldn't throw exceptions when they happen. At the same time, you should structure your code so it does not depend on returning error codes - how do you do this? Good question and it depends on the application, architecture and expected behaviour.
1

I generally will use exceptions as a very last resort. If it is something that should never happen and the language support its, i'll use an assert of some sort (since it happening is surely a bug of some kind).

If it can be avoided, i'll prefer an error code.

There are of course some cases in some languages where you have little choice. For example, c++ constructors can have no return value, and it is probably bad to have a partially constructed object, so throwing an exception is sometimes the best option.

All in all, if you can test for the error condition and simply report the error to the caller in a non-exception based way, I'd prefer that.

Comments

1

Here is my rule of thumb

  • normal program logic - use return codes and parameters
  • something out of the ordinary has happened - use an exception.

Something out of the ordinary is when servers or some other expected hardware or resource is unavailable. When an exception occurs, you want a developer or first line support alerted that something needs fixing.

Comments

1

Use exceptions when something really bad or unexpected happens. Generally, you want to throw an exception when you have an unrecoverable error that can leave the state of your code in an inconsistent state. Sometimes you also want to throw an exception to halt the creation of an object when the constructor has been passed a non-valid argument (IllegalArgumentException). These are all exceptional cases.

As other comments have said, use exceptions sparingly and as a last resort. In Java, throwing an exception pauses the JVM, so you don't want to use for normal error handling as it will greatly decrease the performance of your application.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.