Lazycoder

30Mar/054

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

  • http://weblogs.asp.net/Alex_Papadimoulis Alex Papadimoulis

    So … we need to ensure our applications use a small percentage of the CPU because a sysadmin may load Doom 3 on the machine? Riiiighht ….

  • http://www.lazycoder.com Scott

    Yes, because if your application happens to be an IM client or a P2P application the user will probably leave it running in the background while they play a game. If their game stutters, do you really want them looking at task manager and seeing your application using up their precious CPU cycles?

    There’s a forest in all those trees you know.

  • http://weblogs.asp.net/alex_papadimoulis/archive/2005/04/01/396734.aspx Alex Papadimoulis

    We’re not talking about end-user client apps … you specifically said “Or the sysadmin loads up Doom3 on the *server*”.

    Programming toys and business apps are totally different worlds.

    Anyway, checkout my new post on business exception handling. I’d be interested to hear your feedback.

  • Scott

    hehehehe, good point. I should have made myself more clear.