Why are Exceptions not Checked in .NET?

In the article The Trouble with Checked Exceptions and in Anders Hejlsberg’s (designer of the C# language) own voice, there are three main reasons for C# not supporting checked exceptions as they are found and verified in Java:

  • Neutral on Checked Exceptions

    “C# is basically silent on the checked
    exceptions issue. Once a better
    solution is known—and trust me we
    continue to think about it—we can go
    back and actually put something in
    place.”

  • Versioning with Checked Exceptions

    “Adding a new exception to a throws
    clause in a new version breaks client
    code. It’s like adding a method to an
    interface. After you publish an
    interface, it is for all practical
    purposes immutable, …”

    “It is funny how people think that the
    important thing about exceptions is
    handling them. That is not the
    important thing about exceptions. In a
    well-written application there’s a
    ratio of ten to one, in my opinion, of
    try finally to try catch. Or in C#,
    using statements, which are
    like try finally.”

  • Scalability of Checked Exceptions

    “In the small, checked exceptions are
    very enticing…The trouble
    begins when you start building big
    systems where you’re talking to four
    or five different subsystems. Each
    subsystem throws four to ten
    exceptions. Now, each time you walk up
    the ladder of aggregation, you have
    this exponential hierarchy below you
    of exceptions you have to deal with.
    You end up having to declare 40
    exceptions that you might throw.…
    It just balloons out of control.”

In his article, “Why doesn’t C# have exception specifications?”, Anson Horton (Visual C# Program Manager) also lists the following reasons (see the article for details on each point):

  • Versioning
  • Productivity and code quality
  • Impracticality of having class author differentiate between
    checked and unchecked exceptions
  • Difficulty of determining the correct exceptions for interfaces.

It is interesting to note that C# does, nonetheless, support documentation of exceptions thrown by a given method via the <exception> tag and the compiler even takes the trouble to verify that the referenced exception type does indeed exist. There is, however, no check made at the call sites or usage of the method.

You may also want to look into the Exception Hunter, which is a commerical tool by Red Gate Software, that uses static analysis to determine and report exceptions thrown by a method and which may potentially go uncaught:

Exception Hunter is a new analysis
tool that finds and reports the set of
possible exceptions your functions
might throw – before you even ship.
With it, you can locate unhandled
exceptions easily and quickly, down to
the line of code that is throwing the
exceptions. Once you have the results,
you can decide which exceptions need
to be handled (with some exception
handling code) before you release your
application into the wild.

Finally, Bruce Eckel, author of Thinking in Java, has an article called, “Does Java need Checked Exceptions?”, that may be worth reading up as well because the question of why checked exceptions are not there in C# usually takes root in comparisons to Java.

Leave a Comment