Friday, January 4, 2008

Unexpected exceptions in .NET

You can throw any exception you like from any place of your code in C#. That is quite OK. The problem is that the caller may receive any type of exception. One cannot be sure with .NET framework either. Although there is MSDN documentation on what exceptions can be thrown from what methods, you may receive unexpected expections. For example XmlDocument.Load promises to throw XmlException on parsing error, but if you use validating XmlReader, the XmlSchemaValidationException can be thrown.


I like Java throws clause. You just discover all possible types of exceptions by compiling. With that knowledge, you may handle them or explicitly mark them to be passed to your callers. Thus you won't be surprised in run time (possibly on production system).

No comments: