Thursday, October 26, 2006

Why exception so verbose?

I remember, on my first days with VS.NET, I wanted to add some exception classes to my project. I launched the Add New Item dialog trying to find some item like exception. There was no such item (it even did not have an item for interface! - VS2005 fixed this). I had to add a regular class, typed in the code for its standard constructors, then copied and pasted to create other exception classes. Although my application used a bunch of exceptions, none of them contained additional fields or methods - just the standard constructors. The way current languages support exception make exception's type so important and often solely adequate.

Later, I figured out how to add a template into VS.NET and made it appear on the Add New Item dialog. Since then, I did not have to manually type the exception code again (except for the infrequent case when I want to add some extra stuff into the exception).

However, every time I look at the code of a "standard" exception, I keep asking myself why on earth it has to be so verbose? Wouldn't it be neater to be one-line concise like this:

public exception MyException;

or if the exception should have a parent other than System.Exception:

public exception MySpecificException: MyException;

The compiler will compiles it into a "standard" exception. This is essentially the way delegate keyword in C# works.

Then you and me - the code typists - will have some time for a cup of tea.

No comments: