Exception handling or result code

In response to comments on my article entitled “EJB Exception Handling”:http://coding.mu/archives/2003/09/03/ejb_exception_handling about which one of _exception handling_ and _result code_ is better to indicate errors, here are my opinions on the matter.

Put simply, I prefer the solution that is less intrusive and more natural. In this regard, exception handling has the upper hand in my book.

With exception handling, a programmer can concentrate on implementing the business logic without having to worry about checking for errors at every step of a program. This task is left to be handled by the JVM. This works very well whether the programmer has taken care of handling the errors or not. Simply speaking, when an error occurs, an exception is raised and the JVM checks if there is code to handle it. If there is, that code is executed; if not, the JVM handles the exception in a default way. The program does not proceed and, therefore, does not become inconsistent.

On the other hand, with result codes, a programmer is forced to check the outcome of every function calls to make sure that the expected result is obtained and the application can go to the next step. If all the necessary checks are there, everything is fine. However, if some are missing and an unexpected result code is returned, it will not be handled and the next step in the program will be executed regardless, potentially resulting in a disaster.

So, if I am writing a program in a language that provides exception handling, that is what I will use. If the language does not, then using result codes will work just fine, but with a serious impact on development speed as more checks will have to be implemented.

As I wrote in my reply to one comment, both methods are used in a lot of applications and they work well nonetheless. Exception handling seems to be the standard in languages that support it. It all boils down to how maintainable and readable a programmer wants his or her code to be. By adhering to the standard, he/she has a better chance of being understood by others.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: