Peace For All

June 4, 2006

Why Checked Exceptions are a Good Thing(tm)

Filed under: C#, Programming, technology — Devlin Bentley @ 2:13 am

I am playing around with C#.  Win32/C# class and all that.

I am opening a Graphics file.   A dialog pops up, the user selects a file.  If the user hits OK, I construct a new Bitmap using the file.

That latter bit is copied straight from the MSDN article on the same topic.

Now I <i>now</i> that this is wrong.  All the user has to do is select an invalid file type and exceptions will get thrown all to heck.  They won't be CRITICAL exceptions mind you, not for this simple application, but the application will keep on functioning, assuming that a valid file has been opened.

This is stupid.  Why the hell can't the compiler say "Hey, Devlin, by the way, that constructor there can throw an exception."

I know it can.  Do I want to go and dig through MSDN finding the exact exception name?  Not really.  For whatever reason, IntelliSense is not popping up a list of exceptions that can be thrown by the Bitmap constructor.

What do i want to happen?  I want to hit a hot key, have basic exception handling code thrown up for me, and then go in there and customize the handling.

Yah, sure, I just turned in 30 pages of Java code for class on Friday, and yah, sure, I would estimate that about 8 to 10 pages of that (at least!) is exception handling.  This is because I went about and handled my exceptions <i>properly</i> (informing the user which of their actions is causing a problem and how to remedy the problem, for instance), and got some good information pushed out for myself, the developer, as well at the same time.

And yes, a lot fo that execption handling code is for situations that I <i>personally</i> know will never happen.  Invalid IO exceptions in case arguments passed into a constructor are invalid, in cases where I had just created the object that was being passed in, so I <i>knew</i> that it was valid.

Yah well, I still feel more comfortable handling <b>extra</b> exceptions than not handling <b>enough</b> exceptions.  Requiring the programmer to explicitly break their stride and go in too add every single exception that could happen is just ludicrius.  Especially when the IDE can do it for me!

I am sure that there is some button in Visual Studio to do exactly this, but why shouldn't the compiler bug me if I haven't hit it yet?  Worst case worst, I hit compile and ship the software out the door, and <i>it breaks with ease</i>

The cost benefit trade off of checked exceptions is minimal.  Visual annoyance in return for more stable code.

Finally, I must say this.  It requires discipline to properly catch checked exceptions.  But I believe that it requires even more discipline to catch unchecked exceptions.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: