Home   Cover Cover Cover Cover

Using exception handling

Question: Discuss the advantages and disadvantages of exception handling in C# and contrast them with a primitive error-handling technique in which each called method returns an error code which must be tested by the caller.

Answer: In a language that does not have exception handling, every method must return an error code, which can then be handled by the caller, for example:

  errCode = M1(...);
  if (errCode == 0) {
    errCode = M2(...);
    if (errCode == 0) {
      errCode = M3(...);
      if (errCode != 0) Error(...);
    } else Error(...);
  } else Error(...);

In contrast to this example, exception handling has the following advantages:

  • In the example above, error handling and normal code (i.e. the calls to M1, M2 and M3) can hardly be distinguished. If a try statement is used instead, the error handling and the normal control flow are neatly separated.
  • If the programmer forgets to check an error code in the example above then the error goes undetected. This cannot happen with exception handling. If there is no catch clause for the exception the program is at least aborted and an error message is printed.
  • An exception is an object which can hold information about the kind and location of an error. An error code carries much less information.
  • In the example above every method must be declared with an error parameter or with a function return type for the error code. Such parameters are not needed if exception handling is used.
  • A method can report a variety of errors to the caller. It is complicated to encode all these error kinds in a single error code. If exception handling is used, on the other hand, one can define an exception class for every kind of error and can use a number of catch clauses to distinguish those exceptions.

A possible disadvantage of exception handling is that programming languages which support this mechanism become more complex and programs can become slightly larger (one must declare an exception class and write a try statement as well as one or more throw statements). In general, however, these minor problems are more than outweighed by the advantages of exception handling.

Some people consider it a serious deficiency of C# that a method signature does not define the exceptions that can occur in this method (like in Java). From a software engineering point of view, such a specification would of course be desirable: (1) It shows which exceptions can occur when calling a method and (2) the compiler can make sure that the caller handles those exceptions. However, experience shows, that programmers are lazy and often react to the force of having to write catch clauses by writing empty catch clauses thinking that they will fill them out later, but of course forget to do so. An empty catch clause is worse than no catch clause, because it silently swallows exceptions. In C#, an unhandled exception leads at least to the abortion of the program so that the error is detected.