Lazycoder

3Apr/050

Insideous Exceptions

What Exactly Is An Exceptional Circumstance, Anyway?

Shoot, I didn’t mean to publish this yet. I’m actually going to write something here during my lunch break. My wife decided that yesterday was the Sunday she meant when she said “We need to replace these stairs some Sunday.”. So I spent most of yesterday either staining chairs or cutting treads and risers. wheee.

OK, lunchtime!

The example that Alex picked is really insideous. First, the solution that one of the commentors presented, validating the submitted bids time against an Auction business objects start and end date properties works great except in one instance. Where the bid is submitted before the exact end time but end up in the database after the Auctions end time. You can say, “that’ll never happen. It only takes a few milliseconds to execute a query.”. Right, but the context of an auction site means that this situation COULD occur pretty often.

At an auction site people are trying to submit the winning bid as close to the end time of the auction as possible so that their bid is the final bid. So near the end of an auctions run, you’ll get a LOT of bid submissions all at once. So of course you are using transactions in your data layer so that you maintain the order in which the bids are submitted right? :) Ironically enough, the auction site context means that, if you are throwing an exception every time a bid fails, that you could get a WHOLE LOT OF EXCEPTIONS being thrown all at once. Which is what you don’t want. I’d use return codes in this instance. If you are throwing an exception when a bid fails, you’re coupling your business logic to the database.

BradC in the comments says:

“Here’s the issue, as I see it: Chris’s technique requires a double-trip to the database.
First: to get the “current price and status” of the auction. (to compare and validate)
Second: to actually submit the new bid. “

You don’t have to make two trips to the database, at least not for every bid sumitted. Even if you don’t create a a specific Auction business object, you can still cache the start and end times for the auctions in the application cache or somewhere in memory so you don’t have to go back to the database every bid submission.

Filed under: Tutorials Leave a comment