tags:

views:

1861

answers:

13

When should I create a checked exception, and when should I make a runtime exception?

For example, suppose I created the following class:

public class Account {
    private float balance;

    /* ... constructor, getter, and other fields and methods */

    public void transferTo(Account other, float amount) {
        if (amount > balance)
            throw new NotEnoughBalanceException();
        /* ... */
    }
}

How should I create my NotEnoughBalanceException? Should it extend Exception or RuntimeException? Or should I just use IllegalArgumentException instead?

+3  A: 

IMHO, it shouldn't be an exception at all. An exception, in my mind, should be used when exceptional things happen, and not as flow controls.

In your case, it isn't at all an exceptional status that someone tries to transfer more money than the balance allows. I figure these things happen very often in the real world. So you should program against these situations. An exception might be that your if-statement evaluates the balance good, but when the money is actually being subtracted from the account, the balance isn't good anymore, for some strange reason.

An exception might be that, just before calling transferTo(), you checked that the line was open to the bank. But inside the transferTo(), the code notices that the line isn't open any more, although, by all logic, it should be. THAT is an exception. If the line can't be opened, that's not an exception, that's a plausible situation.

IMHO recap: Exceptions == weird black magic.

being-constructive-edit:

So, not to be all too contradictive, the method itself might very well throw an exception. But the use of the method should be controlled: You first check the balance (outside of the transferTo() method), and if the balance is good, only then call transferTo(). If transferTo() notices that the balance, for some odd reason, isn't good anymore, you throw the exception, which you diligently catch.

In that case, you have all your ducks in a row, and know that there's nothing more you can do (because what was true became false, as if by itself), other than log the exception, send a notification to someone, and tell the customer politely that someone didn't sacrifice their virgins properly during the last full moon, and the problem will be fixed at the first possible moment.

less-enterprisey-suggestion-edit:

If you are doing this for your own pleasure (and the case seems to be this, see comments), I'd suggest returning a boolean instead. The usage would be something like this:

// ...
boolean success = transferTo(otherAccount, ONE_MILLION_DOLLARS_EXCLAMATION);

if (!success) {
  UI.showMessage("Aww shucks. You're not that rich");
  return; // or something...
} else {
  profit();
}
// ...
Henrik Paul
Thanks, but you took this much more seriously than I intended. Very informative nonetheless, but it doesn't really address my question. This is a very simple class that I'm doing for a personal budget tracking application, and it's more of a learning project than a full-fledged application.
Hosam Aly
But what do you suggest to use other than the exception? Assuming the account doesn't allow negative balance, what should I do when the parameter would cause this situation?
Hosam Aly
Other than exception, you have only error codes. But exception gives you full object you can construct and return.
Peter Štibraný
Thanks a lot @wolfie. I originally had my method return an instance of class Transaction (although I didn't include it in my sample). Would you recommend checking that the return is not null? I feel that, from the Account perspective, it's a fairly illegal argument. I appreciate your help.
Hosam Aly
@Hosam: If the method returns an object, yes, I'd use a returned null as an indicator of an error-status, instead of throwing an exception.
Henrik Paul
Checked exceptions are the best way to be notified of errors due to race conditions, such as another transaction having captured the funds between the time you checked for them and when you actually tried to transfer them. There's really no point in doing a "pre-check" in a concurrent system.
erickson
+2  A: 

A checked exception means that clients of your class are forced to deal with it by the compiler. Their code cannot compile unless they add a try/catch block.

The designers of C# have decided that unchecked exceptions are preferred.

Or you can follow the C-style and check return values and not throw exceptions.

Exceptions do have a cost, so they shouldn't be used for control flow, as noted earlier. But the one thing they have going for them is that they can't be ignored.

If you decide that in this case to eschew exceptions, you run the risk that a client of your class will ignore the return value or fail to check the balance before trying to transfer.

I'd recommend an unchecked exception, and I'd give it a descriptive name like InsufficientFundsException to make it quite clear what was going on.

duffymo
You don't have to add a try-catch. If a caller isn't able to handle the exception, it should simply declare it and do nothing.
erickson
Somewhere up the line someone has to handle it. Saying that you can't, and passing responsibility up the line, is making a decision too. You do force them to acknowledge the need for handling and choose.
duffymo
+24  A: 

There's s LOT of disagreement on this topic. At my last job, we ran into some real issues with Runtime exceptions being forgotten until they showed up in production (on agedwards.com), so we resolved to use checked exceptions exclusively.

At my current job, I find that there are many who are for Runtime exceptions in many or all cases.

Here's what I think: Using CheckedExceptions, I am forced at compile time to at least acknowledge the exception in the caller. With Runtime exceptions, I am not forced to by the compiler, but can write a unit test that makes me deal with it. Since I still believe that the earlier a bug is caught the cheaper it is to fix it, I prefer CheckedExceptions for this reason.

From a philosophical point of view, a method call is a contract to some degree between the caller and the called. Since the compiler enforces the types of parameters that are passed in, it seems symmetrical to let it enforce the types on the way out. That is, return values or exceptions.

My experience tells me that I get higher quality, that is, code that JUST WORKS, when I'm using checked exceptions. Checked exceptions may clutter code, but there are techniques to deal with this. I like to translate exceptions when passing a layer boundary. For example, if I'm passing up from my persistence layer, I would like to convert an SQL exception to a persistence exception, since the next layer up shouldn't care that I'm persisting to a SQL database, but will want to know if something could not be persisted. Another technique I use is to create a simple hierarchy of exceptions. This lets me write cleaner code one layer up, since I can catch the superclass, and only deal with the individual subclasses when it really matters.

Don Branson
Very good points. Thanks a lot.
Hosam Aly
+1 We're in agreement, explained slightly different ways.
Bob Cross
Thank you I As starting to think I was the only one with this point of view !!
Newtopian
+2  A: 

Line isn't always clear, but for me usually runtime exceptions = programming errors, checked exceptions = external errors. This is very rough categorization though. Like others say, checked exceptions force you to handle, or at least think for a very tiny fraction of time, about it.

Peter Štibraný
+5  A: 

My feeling is that the checked exception is a useful contract that should be used sparingly. The classic example of where I think a checked exception is a good idea is an InterruptedException. My feeling is that I do want to be able to stop a thread / process when I want it to stop, regardless of how long someone has specified to Thread.sleep().

So, trying to answer your specific question, is this something that you absolutely want to make sure that everyone deals with? To my mind, a situation where an Account doesn't have enough money is a serious enough problem that you have to deal with it.

In response to Peter's comment: here's an example using InterruptedException as concrete case of an exception that should be handled and you need to have a useful default handler. Here is what I strongly recommend, certainly at my real job. You should at least do this:

catch (InterruptedException ie) {
    Thread.currentThread().interrupt();
}

That handler will ensure that the code catches the checked exception and does exactly what you want: get this thread to stop. Admittedly, if there's another exception handler / eater upstream, it's not impossible that it will handle the exception less well. Even so, FindBugs can help you find those.

Now, reality sets in: you can't necessarily force everyone who writes an exception handler for your checked exception to handle it well. That said, at least you'll be able to "Find Usages" and know where it is used and give some advice.

Short form: you're inflicting a load the users of your method if you use a checked exception. Make sure that there's a good reason for it, recommend a correct handling method and document all this extensively.

Bob Cross
Unfortunately so many people don't understand InterruptedException :-( I too believe it should be checked, but many people just ignore it by empty catch block.
Peter Štibraný
Personally, I think InterruptedException is a great example of where an *unchecked* should have been used.
Software Monkey
How could that possibly be better? If you are in the middle of something like a get() without a timeout and you receive an InterruptedException, you are expected to interrupt the thread. A checked exception is a pointed reminder that this is an exceptional and important case that you MUST handle.
Bob Cross
+2  A: 

Simply put, use checked exception only as part of external contract for a library, and only if the client wants/needs to catch it. Remember, when using checked exception you are forcing yourself on the caller. With runtime exception, if they are well-documented, you are giving the caller a choice.

It is a known problem that checked exceptions are over-used in Java, but it doesn't mean that they are all bad. That's why it is such in integral part of the Spring philosophy, for example (http://www.springsource.org/about)

Yoni
any reason for the down vote?
Yoni
+2  A: 

The advantage of checked exceptions is that the compiler forces the developer to deal with them earlier. The disadvantage, in my mind anyway, is that developers tend to be lazy and impatient, and stub out the exception-handling code with the intention of coming back and fixing it later. Which, of course, rarely happens.

Bruce Eckel, author of Thinking in Java, has a nice essay on this topic.

Jason Day
+2  A: 

I don't think the scenario (insufficient funds) warrants throwing an Exception --- it's simply not exceptional enough, and should be handled by the normal control flow of the program. However, if I really had to throw an exception, I would choose a checked exception, by extending Exception, not RuntimeException which is unchecked. This forces me to handle the exception explicitly (I need to declare it to be thrown, or catch it somewhere).

IllegalArgumentException is a subclass of RuntimeException, which makes it an unchecked exception. I would only consider throwing this if the caller has some convenient way of determining whether or not the method arguments are legal. In your particular code, it's not clear if the caller has access to balance, or whether the whole "check balance and transfer funds" is an atomic operation (if it isn't then the caller really has no convenient way of validating the arguments).

EDIT: Clarified my position on throwing IllegalArgumentException.

Zach Scrivena
Thanks @Zach. I've edited my question to clarify that the class provides a getter to retrieve the value. So your recommendation is to throw an IllegalArgumentException? Or would you recommend creating my own unchecked exception?
Hosam Aly
How would you handle a race condition?
erickson
@erickson: I don't understand the question. Could you elaborate please?
Zach Scrivena
@Hosam: I don't recommend throwing an IllegalArgumentException because it's a runtime unchecked exception. For this scenario, a checked exception (e.g. a custom one) is more suitable because you have to handle it explicitly. Better yet, don't use exceptions at all.
Zach Scrivena
I think you've addressed my question with the edit about atomicity. There are cases with no way for a caller to check that an action will succeed before requesting the action, due to a race. E.g., I can test whether a file exists, but another process can delete it before I can open it.
erickson
+3  A: 

My rule is

  • if statements for business logic errors (like your code)
  • cheched exceptions for environment errors where the application can recover
  • uncheched exception for environment errors where there is no recovery

    1. Example for checked exception: Network is down for an application that can work offline
    2. Example for uncheched exception: Database is down on a CRUD web application.

There is much documentation on the subject. You can find a lot by browsing the Hibernate web pages since they changed all exceptions of Hibernate 2.x from checked to unchecked in version 3.x

kazanaki
Recoverability is in the "eye" of the caller; i.e., the implementor has no business presuming what the client can or can't recover from. The convention is that programming errors result in runtime exceptions, while the rest are checked.
erickson
+8  A: 

In general, I think the advice by Joshua Bloch in Effective Java best summarises the answer to your question: Use checked expections for recoverable conditions and runtime exceptions for programming errors (Item 58 in 2nd edition).

So in this case, if you really want to use exceptions, it should be a checked one. (Unless the documentation of transferTo() made it very clear that the method must not be called without checking for sufficient balance first by using some other Account method - but this would seem a bit awkward.)

But also note Items 59: Avoid unnecessary use of checked exceptions and 57: Use exceptions only for exceptional conditions. As others have pointed out, this case may not warrant an exception at all. Consider returning false (or perhaps a status object with details about what happened) if there is not enough credit.

Jonik
I think that in Java there should be no methods returning a `boolean` to indicate success or failure. The chance is just too high that the caller may forget to check the returned value. Throwing an exception in this case seems more appropriate, and this exception may then carry your suggested status object.
Roland Illig
@Roland: Fair point; no strong opinion about that so I'm not gonna argue. :-) What I'm mainly saying is that *if* some "NotEnoughBalanceException" is thrown here, it should be certainly be checked, as per the rule of thumb I quoted.
Jonik
@Roland: I'd disagree with your blanket assertion but not with your principle. :-) It's no different from Jonik's Bloch quote: use exceptions for exceptional conditions. If the method might reasonable "fail" in normal operation (e.g. Collection.add) then a success-valued return is sensible. If failure is a result of unexpected coditions (e.g. saving to file) than an exception is appropriate. Whether bad developers can shoot themselves in the foot is a marginal concern, because they'll find a way regardless.
Andrzej Doyle
+9  A: 

When to use checked exceptions? Honestly? In my humble opinion... never. I think it's been about 6 years since I last created a checked exception.

You can't force someone to deal with an error. Arguably it makes code worse not better. I can't tell you the number of times I've come across code like this:

try {
  ...
} catch (IOException e) {
  // do nothing
}

Whereas I have countless times written code like this:

try {
  ...
} catch (IOException e) {
  throw new RuntimeExceptione(e);
}

Why? Because a condition (not necessarily IOException; that's just an example) wasn't recoverable but was forced down my throat anyway and I am often forced to make the choice between doing the above and polluting my API just to propagate a checked exception all the way to the top where it's (rightlfully) fatal and will be logged.

There's a reason Spring's DAO helper classes translate the checked SQLException into the unchecked DataAccessException.

If you have things like lack of write permissions to a disk, lack of disk space or other fatal conditions you want to be making as much noise as possible and the way to do this is with... unchecked exceptions (or even Errors).

Additionally, checked exceptions break encapsulation.

This idea that checked exceptions should be used for "recoverable" errors is really pie-in-the-sky wishful thinking.

Checked exceptions in Java were an experiment... a failed experiment. We should just cut our losses, admit we made a mistake and move on. IMHO .Net got it right by only having unchecked exceptions. Then again it had the second-adopter advantage of learning from Java's mistakes.

cletus
I agree with this; I have used runtime exceptions exclusively over an extensive codebase (about 2000 classes evenly split between library and application) and have not had practical problems in production with unhandled exceptions.
Software Monkey
Cletus: +1 for a good answer over, but I think the linked article by ckwop is complete crap - my advice would be to remove it. Seriously. Simon (ckwop) recommends we go back to errors indicated by return codes, for heavens sake!
Software Monkey
Unchecked exceptions make your abstractions leak... if you have them. If your design is relatively unstable, and calls for more concrete code, unchecked exceptions fit alright. In a stable, abstract layer of your system, checked exceptions preserve the abstraction.
erickson
Unchecked exceptions are no more or less leaky than checked exceptions. Declaring your code throws an IOException or a SQLException says something about your implementation so you can have a special checked exception but you can have a special unchecked exception too. It's no different.
cletus
If a service layer declares a UserAlreadyExistsException, the client can catch that and recover. If the data access layer is simply letting unchecked Hibernate ConstraintViolationExceptions bubble up, any caller that wants to recover from that specific problem has a compile-time dependency on a particular implementation. Checked exceptions encourage implementations of abstractions to re-write low level exceptions into a form appropriate for the abstraction, preventing implementation leakage.
erickson
UserAlreadyExistsException can be an unchecked exception too. The client can still catch it. The only difference is they aren't forced to deal with it when they might have no interest in catching it.
cletus
cletus - What's interesting about that "exceptions break encapsulation" link is that the majority of his objections (including the eponymous one) are invalidated **if you use checked exceptions**. Exception throwing *isn't* invisible if they're checked. Exceptions *don't* break encapsulation if the method declares it throws its exception as part of its contract. And if a developer can't be trusted to handle checked exceptions correctly, with the compiler's help, why on earth would they be expected to handle unchecked exceptions correctly?
Andrzej Doyle
Fundamentally the only difference between checked exceptions and unchecked exceptions is that the compiler **forces** you to document checked exceptions. You should be documenting any exceptions your code can throw *anyway*; I can't see why anyone would be against this being statically checkable to prevent it from being stale or from any exceptions being missed out. Exceptions *are* part of your API whether checked or not; checked just ensures you declare your API accurately.
Andrzej Doyle
+2  A: 

I recently had a problem with exceptions, code threw NullPointerException and I had no idea why, after some investigation it turned out that real exception was swallowed(it was in new code, so its still being done) and method just returned null. If you do checked exceptions you must understand that bad programmers will just try catch it and ignore exception.

01
A: 

Myself, I prefer using checked exceptions as I can.

If you are an API Developer (back-end developer), use checked exceptions, otherwise, use Runtime exceptions.

Also note that, using Runtime exceptions in some situations is to be considered a big mistake, for example if you are to throw runtime exceptions from your session beans (see : http://m-hewedy.blogspot.com/2010/01/avoid-throwing-runtimeexception-from.html for more info about the problem from using Runtime excpetions in session beans).

Mohammed