By defining RecoveredException and UnrecoveredException, we could determine how we would recover from an error based on these types.Further, we subclassed exceptions for two reasons:

  • to determine how to convert the exception to another type (in catch blocks),
  • and, to store information in the exception instance, so that it can be propagated to the callers.

Subclassing for these cases was effective in these cases, because:

  • we could use catch blocks as if statements that branched based on exception class type,
  • and, in type-safe languages the compiler would warn us, if we tried to get attributes of the wrong class.

However, with languages without type-safety, we don’t have the compiler checks. We could just as well:

  • use an integer or enum attribute to represent the exception subtype of a RecoveredException or UnrecoveredException,
  • and use a dictionary to add attributes to the exception instance.

Using integers or enum attributes have the benefit of being able to use switch statements for conversion of exceptions (unless it’s Python).

Dictionaries are useful for generating messages for the UI. For instance, by default, Python has a string formatting feature that is written as follows:

message = u"Syntax error in line {lineNumber} of the file '{filename}'".format(**exceptionAttributes)

where exceptionAttributes is a dictionary of key/value pairs for the exception. This statement assigns a string to the variable message with values for {lineNumber} and {filename} taken from exceptionAttributes. A feature like this is useful in translation, since the order of the attributes may show up in a different order when the locale changes. In Japanese, it would be as follows:

message = u"ファイル'{filename}'の{lineNumber}行目において、文法エラーが発生しました。".format(**exceptionAttributes)

Such feature can be implemented in other languages too. Libraries based on ICU, such as AngularJS for Javascript, have this functionality and much more.

Contents

Background

Review of Exception Handling Basics

The Requirements

The Three-tiered Exception Handling Architecture

Summary