CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

Exceptions vs. Error Codes

This is something that I've wondered about.  This post here sums it up well.  In the comments, Andy writes :

I totally agree. It can be summed up into one key rule: "never use exceptions for applicational logic or flow control"

-Brendan



Comments

Brendan Tompkins said:

Okay, so I've always thought that returning an int to indicate success/failure was cheesy. The thing that is attractive about using exceptions, is that you can be more expressive about what happened. But then again, using an exception for normal conditions seems to be forcing a round peg into a square hole.

IMO, Status codes are great if your status can be boiled down to true or false. (public bool AttemptSomething()) If you need to be more expressive that, what works best? If the error condition is expected, how do you notify your caller? Returning an more complex object of some type? Is this just a design issue, and by that I mean with good design this isn't needed?
# August 25, 2004 8:49 AM

Darrell said:

It depends. Basically your method should do what its name indicates it will do. If it cannot, then it throws an exception. But your method should be able to do what it says it will do most of the time. :)

If the error condition is likely to happen, such as attempting to gain a thread lock, then you should name your method TryAcquireLock, for example. This is the naming convention that v2 of the Framework is implementing. Methods named like this can return a bool to indicate true (we got the lock) or false if not. You would not want to try that method often if the chance of failure was 50% or so, your perf would die.

So I would say I agree with your comment. :) If it gets much more complex, then custom business objects need to be created or extended to handle the more complex error handling. All of this is, of course, my humble opinion.
# August 25, 2004 9:28 AM

Darrell said:

Correction: "You would not want to try that method often USING EXCEPTIONS if the chance of failure was 50% or so, your perf would die."
# August 25, 2004 9:29 AM

Brendan Tompkins said:

This is good stuff. Thanks Darrell.
# August 25, 2004 9:34 AM

Darrell said:

We all try to help each other out as best we can! :)
# August 25, 2004 9:37 AM

Brendan Tompkins [admin] said:

Today, I ran into a situation where I needed to validate a business rule within a business object. ...
# October 14, 2005 4:17 PM

Brendan Tompkins [admin] said:

Today, I ran into a situation where I needed to validate a business rule within a business object. ...
# October 14, 2005 4:18 PM

Brendan Tompkins [admin] said:

Today, I ran into a situation where I needed to validate a
business rule within a business object. ...
# October 14, 2005 5:16 PM

Brendan Tompkins [admin] said:

Today, I ran into a situation where I needed to validate some
Business Rules (BRs) within a business...
# October 14, 2005 10:28 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!