When to throw an Exception
Hi, My Name is Alex, and I am a Resource Waster
Right, so what happens when your 10 page application, which is sustaining lets say 518,000 unique users per day, starts throwing an exception every time a page loads? What happens if 1000 of the 2500 processes running on the same server as your application start throwing 5000 exceptions per second?
Also consider that you don’t always have the FULL resources of the machine available to your application. Your application might work fine if it’s the only application running on the desktop or the server. What happens if 1000 of the 2500 processes running on the same machine as your application start throwing 5000 exceptions per second? Or the sysadmin loads up Doom3 on the server? Math is hard and exponential wastes of resources suck. Guys like Alex are the reason that I need a half-a-gig of RAM to run Word.
That being said, I’m from the camp that says “use exceptions for exceptional situations”. So if my applications is throwing an exception, it will damn well be for a catastrophic reason and the performance of the application will be coming to a dead stop for whatever reason triggered the exception. Say the database server isn’t available or some idiot decided to hot swap the drive that is holding the users uploads.
“If your database stored procedure yells “Documents status may not be altered while an approval is pending,” exceptions are the simplest way to wrap that up, jump across your tiers, and display the message directly to the client. Any other way of doing it would be needlessly more complicated.”
THAT is not exceptional, that is a business rule. Another application deleting the document whos status your stored procedure is attempting to update before your SQL transaction starts is a good place to throw an exception.



Pingback: Alex Papadimoulis' WebLog