A language extension for Error Handling


Modern Languages use exceptions for error handling. The big plus of exceptions is that they provide, if you will, a kind of additional return value that is handled differently from the regular return value. In older languages such as C, the return type of a function is often used to return an error code, and if actual data should be returned, it has to be done through an out parameter. This is awkward. C provides another way, of course, with is the errno global variable. A function can set errno and a caller can inspect this variable directly after calling a function. But there is no information in a function's signature which errors it may raise and the IDE cannot force the programmer to write code to handle them. Exceptions (depending on their specific implementation) can provide this feature. A big disadvantage of exceptions, however, is that they require longjumps "up the stack". These are expensive and hance often not used in embedded software.

Based on these constraints we have designed a compromise feature for error handling into C:

  • Error types are declared in way similar to constants
  • Functions declare the errors they may raise
  • Only those declared errors can actually be raised by the implememtation
  • The caller is required to handle the error with a try/catch-like syntax

The feature is implemented by transparently adding an out variable to the function, so no long jump is required -- but of course the "exceptions" cannot propagate up the stack and must be handled locally.

Currently this only works for functions, but we plan on adding this to components and interfaces as well. Note that, as usual, this is an independent language extension and can be used optionally. The feature resides in the com.mbeddr.core.util language.