35

It's my understanding that common wisdom says to only use exceptions for truly exceptional conditions (In fact, I've seen that statement here at SO several times).

However, Krzysztof Cwalina says:

One of the biggest misconceptions about exceptions is that they are for “exceptional conditions.” The reality is that they are for communicating error conditions. From a framework design perspective, there is no such thing as an “exceptional condition”. Whether a condition is exceptional or not depends on the context of usage, --- but reusable libraries rarely know how they will be used. For example, OutOfMemoryException might be exceptional for a simple data entry application; it’s not so exceptional for applications doing their own memory management (e.g. SQL server). In other words, one man’s exceptional condition is another man’s chronic condition.

He then also goes on to say that exceptions should be used for:

  • Usage errors
  • Program errors
  • System failures

Considering Krzysztof Cwalina is the PM for the CLR team at MS I ask: What do you think of his statement?

Esteban Araya
  • 29,284
  • 24
  • 107
  • 141

15 Answers15

27

This sounds over-simplistic, but I think it makes sense to simply use exceptions where they are appropriate. In languages like Java and Python, exceptions are very common, especially in certain situations. Exceptions are appropriate for the type of error you want to bubble up through a code path and force the developer to explicitly catch. In my own coding, I consider the right time to add an exception when the error either can't be ignored, or it's simply more elegant to throw an exception instead of returning an error value to a function call etc.

Some of the most appropriate places for exceptions that I can think of offhand:

  • NotImplementedException - very appropriate way of designating that a particular method or function isn't available, rather than simply returning without doing anything.
  • OutOfMemory exceptions - it's difficult to imagine a better way of handling this type of error, since it represents a process-wide or OS-wide memory allocation failure. This is essential to deal with, of course!
  • NullPointerException - Accessing a null variable is a programmer mistake, and IMO this is another good place to force an error to bubble to the surface
  • ArrayIndexException - In an unforgiving language like C, buffer overflows are disastrous. Nicer languages might return a null value of some type, or in some implementations, even wrap around the array. In my opinion, throwing an exception is a much more elegant response.

This is by no means a comprehensive list, but hopefully it illustrates the point. Use exceptions where they are elegant and logical. As always with programming, the right tool for the right job is good advice. There's no point going exception-crazy for nothing, but it's equally unwise to completely ignore a powerful and elegant tool at your disposal.

Abel
  • 56,041
  • 24
  • 146
  • 247
Jay
  • 41,768
  • 14
  • 66
  • 83
  • 1
    +1, I enjoy implementing exceptions in my own code, in order to flag items that require work (i.e.: NotImplemented), or to get more detailed error reporting from specific errors (i.e.: File IO operations). I leave them to bubble up to a certain level and then email them to myself. Good for testing! – Pat Hermens Oct 08 '08 at 01:18
  • Agreed, error communication by throwing exceptions has its place. – jpoh Oct 08 '08 at 01:49
10

For people who write frameworks, perhaps it's interesting.

For the rest of us, it's confusing (and possibly useless.) For ordinary applications, exceptions have to be set aside as "exceptional" situations. Exceptions interrupt the ordinary sequential presentation of your program.

You should be circumspect about breaking the ordinary top-to-bottom sequential processing of your program. The exception handling is -- intentionally -- hard to read. Therefore, reserve exceptions for things that are outside the standard scenarios.

Example: Don't use exceptions to validate user input. People make input mistakes all the time. That's not exceptional, that's why we write software. That's what if-statements are for.

When your application gets an OutOfMemory exception, there's no point in catching it. That's exceptional. The "sequential execution" assumption is out the window. Your application is doomed, just crash and hope that your RDBMS transaction finishes before you crash.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 4
    Hey man, I don't mean to be rude, but you don't support your statement with any reasons. – Esteban Araya Oct 08 '08 at 00:49
  • That's correct, for frameworks or system development the exception handling is quite different than for applications. – OscarRyz Oct 08 '08 at 00:56
  • @Esteban Araya: good point -- I omitted mentioning that the top-to-bottom sequential text is important. – S.Lott Oct 08 '08 at 00:59
  • 3
    @S.Lott: Thanks for the added points. However, I don't think sequential presentation is necessarily good - especially in an event driven environment. Also, I don't think exception handling is hard to read. But that's just me. Thanks again! – Esteban Araya Oct 08 '08 at 01:07
  • "I don't think sequential presentation is necessarily good". Interesting. Since most programming languages use sequential presentation, either with ;'s or \n's to string statements sequentially. – S.Lott Oct 08 '08 at 01:09
  • @S.Lott: Sorry; I mean't "ordinary top-to-bottom sequential processing of your program". Sequential programming is SO not object oriented. :) – Esteban Araya Oct 08 '08 at 01:13
  • @S.Lott: As for "Don't use exceptions to validate user input, for example. People make mistakes all the time. That's what if-statements are for."; I can agree to that, but only up to a certain point: If the user input is more complicated, so that e.g. a parser has to be started to check it, then IMHO it's perfectly valid for this parser to throw an exception if the input is invalid (see also http://www.codeproject.com/KB/cpp/cppexceptionsproetcontra.aspx#3_5, fourth paragraph in section 3.6); what I'm trying to say is that also here, it depends very much on the circumstances! – codeling Jan 02 '12 at 10:11
  • @nyarlathotep: I find that it depends on the language. For Python, for example, the exception is almost required. – S.Lott Jan 02 '12 at 13:44
  • 6
    Explaining my downvote, I completely disagree with every single point you made. This one's the biggest, though: *"Exception handling is intentionally hard to read"* WHAT? What makes you think **any** language designer would intentionally make one of it's features hard to read? – coredumperror Sep 19 '14 at 21:28
  • Exception handling in most of the actual languages is elegant and easy to read. – JLuis Estrada Sep 27 '18 at 17:36
9

It is indeed difficult to know what exactly construes an "exceptional condition" which warrants the use of an exception in a program.

One instance that is very helpful for using communicating the cause of errors. As the quote from Krzysztof Cwalina mentions:

One of the biggest misconceptions about exceptions is that they are for “exceptional conditions.” The reality is that they are for communicating error conditions.

To give a concrete example, say we have a getHeader(File f) method that is reading some header from a file and returns a FileHeader object.

There can be several problems which can arise from trying to read data from a disk. Perhaps the file specified doesn't exist, file contains data that can't be read, unexpected disk access errors, running out of memory, etc. Having multiple means of failure means that there should be multiple ways to report what went wrong.

If exceptions weren't used, but there was a need to communicate the kind of error that occurred, with the current method signature, the best we can do is to return a null. Since getting a null isn't very informative, the best communication we get from that result is that "some kind of error happened, so we couldn't continue, sorry." -- It doesn't communicate the cause of the error.

(Or alternatively, we may have class constants for FileHeader objects which indicate FileNotFound conditions and such, emulating error codes, but that really reeks of having a boolean type with TRUE, FALSE, FILE_NOT_FOUND.)

If we had gotten a FileNotFound or DeviceNotReady exception (hypothetical), at least we know what the source of the error was, and if this was an end user application, we could handle the error in ways to solve the problem.

Using the exception mechanism gives a means of communication that doesn't require a fallback to using error codes for notification of conditions that aren't within the normal flow of execution.

However, that doesn't mean that everything should be handled by exceptions. As pointed out by S.Lott:

Don't use exceptions to validate user input, for example. People make mistakes all the time. That's what if-statements are for.

That's one thing that can't be stressed enough. One of the dangers of not knowing when exactly to use exceptions is the tendency to go exception-happy; using exceptions where input validation would suffice.

There's really no point in defining and throwing a InvalidUserInput exception when all that is required to deal in such a situation is to notify the user of what is expected as input.

Also, it should be noted that user input is expected to have faulty input at some point. It's a defensive measure to validate input before handing off input from the outside world to the internals of the program.

It's a little bit difficult to decide what is exceptional and what is not.

Community
  • 1
  • 1
coobird
  • 159,216
  • 35
  • 211
  • 226
6

Since I usually program in Python, and in that language exceptions are everywhere, to me an exception may represent anything from a system error to a completely legitimate condition.

For example, the "pythonic" way to check if a string contains an integer is to try int(theString) and see if it raises an exception. Is that an "exceptional error"?

Again, in Python the for loop is always thought of as acting on an iterator, and an iterator must raise a 'StopIteration' exception when it finishes its job (the for loop catches that exception). Is that "exceptional" by any means?

Federico A. Ramponi
  • 46,145
  • 29
  • 109
  • 133
  • I frequently use Java's Integer.parseInt(String var) in the same way. – slim Jan 04 '09 at 01:59
  • 3
    +1 Came here to say "Depends on the language. In Python...." – J.T. Hurley Jan 04 '09 at 03:29
  • 4
    Note that in Python this may work, but in Java, C++, VB and C#, catching an exception is very slow, between 40 to 400 times slower than an if-statement can be. Depending on your input (do you expect an occasional non-number, or will it happen often), this may or may not be good design. Which is again why exceptions should be used when the expected situation is just that: exceptional as compared to normal. – Abel Jul 19 '10 at 14:15
3

I think the closer to the ground are you are the less appropriate exceptions as a means of error communication become. At a higher abstraction such as in Java or .net, an exception may make for an elegant way to pass error messages to your callers. This however is not the case in C. This is also a framework vs api design decision.

Aaron Fischer
  • 20,853
  • 18
  • 75
  • 116
3

If you practice "tell, don't ask" then an exception is just the way a program says "I can't do that". It is "exceptional" in that you say "do X" and it cannot do X. A simple error-handling situation. In some languages it is quite common to work this way, in Java and C++ people have other opinions because exceptions become quite costly.

General: exception just means "I can't"

Pragmatic: ... if you can afford to work that way in your language.

Citizenship: ... and your team allows it.

Tim Ottinger
  • 1,442
  • 9
  • 5
3

Here is the definition for exception: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

Therefore, to answer your question, no. Exceptions are for disruptive events, which may or may not be exceptional. I love this definition, it's simple and works every time - if you buy into exceptions like I do. E.g., a user submits an incorrect un/pw, or you have an illegal argument/bad user input. Throwing an exception here is the most straightforward way of solving these problems, which are disruptive, but not exceptional, nor even unanticipated.

They probably should have been called disruptions, but that boat has sailed.

boardrider
  • 5,882
  • 7
  • 49
  • 86
hamsterofdark
  • 341
  • 3
  • 4
2

I think there are a couple of good reasons why exceptions should be used to catch unexpected problems.

Firstly, they create an object to encapsulate the exception, which by definition must make it a lot more expensive than processing a simple if-statement. As a Java example, you should call File.exists() rather than routinely expecting and handling a FileNotFoundException.

Secondly, exceptions that are caught outside the current method (or maybe even class) make the code much harder to read than if the handling is all there in in the one method.

Having said that, I personally love exceptions. They relieve you of the need of explicitly handling all of those may-happen-but-probably-never-will type errors, which cause you to repetitively write print-an-error-and-abort-on-non-zero-return-code handling of every method call.

My bottom line is... if you can reasonably expect it to happen then it's part of your application and you should code for it. Anything else is an exception.

Greg Cottman
  • 658
  • 1
  • 5
  • 7
  • Should I also make `File.hasReadPermission()` and `File.isNotCorrupt()` calls every time I want to read a file? – daniel1426 Feb 12 '14 at 18:42
1

I've been wondering about this myself. What do we mean by "exceptional"? Maybe there's no strict definition, but are there any rules of thumb that we can use to decide what's exceptional, in a given context?

For example, would it be fair to say that an "exceptional" condition is one that violates the contract of a function?

JW.
  • 50,691
  • 36
  • 115
  • 143
0

KCwalina has a point. It will be good to identify cases where the code will fail (upto a limit)

I agree with S.Lott that sometimes validating is better than to throw Exception. Having said that, OutOfMemory is not what you might expect in your application (unless it is allocating a large memory & needs memory to go ahead).

I think, it depends on the domain of the application.

shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
0

The statement from Krzysztof Cwalina is a little misleading. The original statement refers 'exceptional conditions', for me it is natural that I am the one who defines what's exceptional or not. Nevertheless, I think the message passed through OK, since I think we are all talking about 'developer' exceptions.

Exceptions are great for communication, but with a little hierarchy design they are also great for some separation of concerns, specially between layers (DAO, Business, etc). Of course, this is only useful if you treat these exceptions differently.

A nice example of hierarchy is spring's data access exception hierarchy.

Miguel Ping
  • 18,082
  • 23
  • 88
  • 136
0

The saying that exceptions should be used for exceptional circumstances is used in "Effective Java Second Edition": one of the best java books.

The trouble is that this is taken out of context. When the author states that exceptions should be exceptional, he had just shown an example of using exceptions to terminate a while loop - a bad exception use. To quote:

exceptions are, as their name implies, to be used only for exceptional conditions; they should never be used for ordinary control flow.

So it all depends on your definition of "exception condition". Taken out of context you can imply that it should very rarely be used.

Using exceptions in place of returning error codes is good, while using them in order to implement a "clever" or "faster" technique is not good. That's usually what is meant by "exceptional condition".

Checked exception - minor errors that aren't bugs and shouldn't halt execution. ex. IO or file parsing Unchecked exception - programming "bug" that disobeys a method contract - ex. OutOfBoundsException. OR a error that makes continuing of execution a very bad idea - ex IO or file parsing of a very important file. Perhaps a config file.

TigerBear
  • 2,479
  • 1
  • 21
  • 24
0

What it comes down to is what tool is needed to do the job.

Exceptions are a very powerful tool. Before using them ask if you need this power and the complexity that comes with it.

Exceptions may appear simple, because you know that when the line with the exception is hit everything comes to a halt. What happens from here though?

Will an uncaught exception occur?

Will the exception be caught by global error handling?

Will the exception be handled by more nested and detailed error handling?

You have to know everything up the stack to know what that exception will do. This violates the concept of independence. That method now is dependent on error handling to do what you expect it to.

If I have a method I shouldn't care what is outside of that method. I should only care what the input is, how to process it, and how to return the response.

When you use an exception you are essentially saying, I don't care what happens from here, something went wrong and I don't want it getting any worse, do whatever needs to be done to mitigate the issue.

Now if you care about how to handle the error, you will do some more thinking and build that into the interface of the method e.g. if you are attempting to find some object possibly return the default of that object if one can't be found rather than throwing some exception like "Object not found".

When you build error handling into your methods interface, not only is that method's signature more descriptive of what it can do, but it places the responsibility of how to handle the error on the caller of the method. The caller method may be able to work through it or not, and it would report again up the chain if not. Eventually you will reach the application's entry point. Now it would be appropriate to throw an exception, since you better have a good understanding of how exceptions will be handled if you're working with the applications public interface.

Let me give you an example of my error handling for a web service.

Level 1. Global error handling in global.asax - That's the safety net to prevent uncaught exceptions. This should never intentionally be reached.

Level 2. Web service method - Wrapped in a try/catch to guarantee it will always comply with its json interface.

Level 3. Worker methods - These get data, process it, and return it raw to the web service method.

In the worker methods it's not right to throw an exception. Yes I have nested web service method error handling, but that method can be used in other places where this may not exist.

Instead if a worker method is used to get a record and the record can't be found, it just returns null. The web service method checks the response and when it finds null it knows it can't continue. The web service method knows it has error handling to return json so throwing an exception will just return the details in json of what happened. From a client's perspective it's great that it got packaged into json that can be easily parsed.

You see each piece just knows what it needs to do and does it. When you throw an exception in the mix you hijack the applications flow. Not only does this lead to hard to follow code, but the response to abusing exceptions is the try/catch. Now you are more likely to abuse another very powerful tool.

All too often I see a try/catch catching everything in the middle of an a application, because the developer got scared a method they use is more complex than it appears.

Timothy Gonzalez
  • 1,802
  • 21
  • 18
0

I think he is right. Take a look at number parsing in java. You cant even check input string before parsing. You are forced to parse and retrieve NFE if something went wrong. Is parse failure something exceptional? I think no.

user124
  • 1,632
  • 1
  • 11
  • 11
0

I certainly believe exceptions should be used only if you have an exceptional condition.

The trouble is in the definition of "exceptional". Here is mine:

A condition is exceptional if it is outside the assumed normal behaviour of the part of the system that raises the exception.

This has some implications:

  • Exceptional depends on your assumptions. If a function assumes that it is passed valid parameters, then throwing an IllegalArgumentException is OK. However if a function's contract says that it will correct input errors in input in some way, then this usage is "normal" and it shouldn't throw an exception on an input error.
  • Exceptional depends on sub-system layering. A network IO function could certainly raise an exception if the network is discommented, as it assumes a valid connection. A ESB-based message broker however would be expected to handle dropped connections, so if it used such a network IO function internally then it would need to catch and handle the error appropriately. In case it isn't obvious, try/catch is effectively equivalent to a subsystem saying "a condition which is exceptional for one of my components is actually considered normal by me, so I need to handle it".
mikera
  • 105,238
  • 25
  • 256
  • 415