views:

351

answers:

10

In languages that support exception objects (Java, C#), when is it appropriate to use error codes? Is the use of error codes ever appropriate in typical enterprise applications?

Many well-known software systems employ error codes (and a corresponding error code reference). Some examples include operating systems (Windows), databases (Oracle, DB2), and middle-ware products (WebLogic, WebSphere). What benefits do error codes provide? What are the disadvantages to using error codes?

+1  A: 

Error codes are old-school. They are of little to no value at all.

The only possible value to an error code is that it can identify a very specific circumstance. You could have a code for each point in the code base that can throw an exception. This would allow you to narrow down very precisely what the problem must be.

But nobody cares about that level of detail. Who wants to maintain such a mess. It would leave you with codes that meant something like "condition A and B but not C due to state S". It's more effort than it's worth to try to work out exactly what that means. A stack trace will be more valuable in telling you where in the program the problem occurred.

I learned to program computers before exceptions were a widespread technique. I'm so glad we got exceptions instead!

John Saunders
Exceptions have no difficult at all carrying detail. Where error codes are appropriate is when the caller might reasonably be asking "Is it possible to XYZ?", e.g. "Is it possible to parse this string as a DateTime?". In this case exceptions are not a good way to report failure. Error codes are best and fastest when failure is handled by the immediate caller. Remember, it's cheaper to turn an error code into an exception than vice versa.
Ben Voigt
The error code returned by, for instance, `TryParse` is not a good example of an error code. It's only true/false. It is unnecessary to assign a separate error code to each possible reason for a method to fail.
John Saunders
-1. Faults/exceptions themselves may contain an error code. "Error codes" may not just be direct method returns.
Robert Fraser
@Robert: I know that an error code may be present. This is especially true when the exception was thrown by a wrapper around a legacy application that uses error codes. Examples are any exceptions from calling Win32 (HRESULT) or COM, or SqlException, which contains SQL Server error codes.
John Saunders
+7  A: 

I don't think I've ever used error codes in .Net except in one situation - when I was creating a console application that I knew was going to be called from another app. This other app had to know when the console app failed, and what went wrong. So, one example of when it would be appropriate would be when you know your program will be called by other programs, and you want a structured way for them to understand errors.

That said, I was a newbie to .NET at the time, and have never used error codes since.

As a side note, as a Windows guy, it's nice to be able to plop in an error code and come up with a KB article, so an error code combined with good documentation and the ability to find it = nice feelings from your users.

David Stratton
+4  A: 

I frequently use error codes when an error needs to be conveyed to the user, since they can be internationalized. For example, in a compiler, if there are errors in user code, errors can be signaled in the compiler backend, while the frontend can localize them into culture/language-specific strings for user consumption. Enums may be better for this purpose than raw integers, however.

I've also used them in creating an "error reporting" framework for the app. When exceptions were thrown, they were thrown with an error code, which, when the exception bubbled up, was sent (with a log) to the central server. The code helped organize the database so we could inspect logs related to a specific error.

Finally, as mentioned in a couple other answers, error codes are easy and language-agnostic to google (think Windows error codes/MS KB articles), so an error code with a description of what went wrong may be better for end-users of a technical product.

The idea of error codes is useful, but IMO they belong as exception members or as parameters to an IErrorReporter interface or something more ofthen than as method return values.

Robert Fraser
+1 for storing error codes as exception members and the idea of an "error reporting" framework. I accepted Loren's answer because he better defined when it is appropriate to use error codes. This answer excellently describes how to best use and implement error codes.
Jim Hurne
+6  A: 

Very common for web service interfaces. It's very easy and standard to return a code with a description.

I agree that for most of the scenarios is old school

I'd say the biggest disadvantages it's the quality of code. You have to add more complex logic to manage error codes while exceptions are bubbled without having to use method parameters or return values. Also have to add an "IF" to check if the returned code is SUCCESS or not, while exceptions goes directly to the error handling block.

Claudio Redi
+2  A: 

C#, and probably Java too, supports a better exception handling control flow, the finally keyword, which makes things a little nicer than using error codes. An exception object can contain any level of detail, certainly much more than an error code. So the exception object is way more practical, but you might run into an uncommon case where an error code would be more appropriate.

FWIW, C++ also supports exception objects. I don't think that C++ supports a finally keyword (though the newer C++ whatevers just might), but in C++ you also have to avoid things like returning inside a catch handler.

Chris O
+9  A: 

WITHIN a program one should always use exceptions instead of error codes. However, exceptions can't propagate beyond a program. Any time the error must leave the program you are left with error messages or error codes.

For simple things that will always be human-operated error messages without codes are fine. You can say "File not found" without giving it an error code. However, if it might be another computer on the other end then you should give error codes in addition. You don't want to break the other system when you change it to "File (x) not found".

Edit: Stupid HTML filter didn't like my bracketed x.

Loren Pechtel
Nicely stated. +1 to you!
David Stratton
Exceptions may propagate out of a web service as faults, so your statement is not accurate.
John Saunders
@John as a generalization it is reasonably accurate.
Rex M
@Rex: it would be more accurate to say, "Exceptions may not always propagate beyond a program".
John Saunders
Thanks! Your criteria for when to use error codes is excellent. The system I had this question in mind for does need to communicate with external systems, so we'll probably use error codes at the boundry.
Jim Hurne
+4  A: 

I'm a newbie to stack overflow but...

I believe that error codes tend to be used or useful for dealing with erroneous situations that require an end-user of sorts to get involved to rectify a situation. If your code is to be maintained by another developer then exceptions is the way to go. However, in a situation where there is a problem:

  • in the environment that your application is running

  • with communication between your app and some other entity (web server, database, socket, etc)

  • that a device or device driver indicates (hardware failure maybe?)

then error codes may make sense. For example, if your app attempted to log into a database on behalf of your end-user, but the DB was unreachable for authentication (DB is off-line, cable is unplugged) then an error code/description combo might help the end-user rectify the problem.

Again at the developer/engineer level who will be able to touch the source code (traditional debugging and testing techniques) and modify it, use exceptions.

Hope this helps...

--jqpdev

jqpdev
A: 

I've written many web services that are consumed by other (remote) applications. When things go badly with a request, customers more or less insist on getting a code, so that they don't have to do some horrific string comparison to find out what went wrong.

Take HTTP result codes as a fine example of this sort of behavior. "200" means happy, "300" could go either way, "400" or "500" means start freaking out.

WineSoaked
Your customers don't deal with SOAP Faults? Interesting.
John Saunders
A lot of these existing services don't use SOAP, and predate my arrival at my firm. I just do what I can with what I was handed.
WineSoaked
+1  A: 

Error codes were designed in an age where the only way for a function to tell the caller that something went wrong was to assign a special meaning to one or more values of those which can be returned, and very frequently only a native integer or so was available for returning that special value.

For instance, in C the "get character" routine returns the next character value in ASCII, but returns a negative value if for some reason something went wrong. You are then responsible for returning to YOUR caller in a way so this error situation can be handled, and that must return etc.

The Exception mechanism is an elegant way to handle this "this is an emergency, we must return from code until something can deal with the problem". Error codes are inferior to this.

Thorbjørn Ravn Andersen
A: 

Error codes are for if you want to send them to the user. If not, use an exception.

DeadMG
Thats not very user friendly, see Loren's answer above.Codes should be fed to other systems, humans should be presented with natural language descriptions rather than ... "WTF is error 666? Sounds bad but I have no idea what it means"
crowne
You can do both--start with an error code and then give a text description: Error 404 -- Page not found.
Loren Pechtel
It's much easier for a user to remember Error 666- especially if these codes are cross-language. If you gave a natural language description, you'd have to translate it into six trillion languages, and when you got them back, you'd still not be sure wtf went wrong.
DeadMG