Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it appropriate to use error codes?

Tags:

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?

like image 548
Jim Hurne Avatar asked May 08 '10 02:05

Jim Hurne


People also ask

What do error codes mean?

The error code is a specific number that identifies what the error is to the system. It also can be helpful in finding a resolution to the problem. If you're getting an error code, search for the error code number and where you're getting the error to find a resolution.

What are the downsides of error codes?

A major drawback of error codes is that they lead to function calls being intertwined with if..else blocks testing for the various error conditions. The result is that program logic is mixed up with error handling. This is not a big issue in the simple example above, but it quickly becomes confusing in real-world code.

Why are exceptions better than error codes?

Error codes are safer for well-reviewed code Error codes mean that you must carefully look at function calls to see if the programmer handled the possible errors. Exceptions mean that you must imagine what happens if an exception is thrown anywhere in the flow.

Why is using exceptions a better idea than returning an error value?

When you code using return codes, you're preparing yourself for failure, and hope your fortress of tests is secure enough. When you code using exception, you know that your code can fail, and usually put counterfire catch at chosen strategic position in your code.


1 Answers

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".

like image 128
Loren Pechtel Avatar answered Nov 14 '22 09:11

Loren Pechtel