hierarchical-exceptions-1.0.1: Template Haskell functions to easily create exception hierarchies

Safe HaskellNone

Control.Exception.Hierarchical

Description

Control.Exception leverages Data.Typeable to fake subtyping and thereby give Haskell support for hierarchies of exceptions. However, defining exception hierarchies requires quite a bit of boilerplate. For instance, to define

  • a top-level exception, TracerException,
  • a sub-exception, TimingFailure, and
  • a sub-exception, WriteFailure,

requires several paragraphs of code:

 import Control.Exception
 import Data.Typeable (Typeable, cast)

 data TracerException = forall e. Exception e => TracerException e
                      deriving Typeable

 instance Show TracerException where
   show (TracerException e) = show e

 instance Exception TracerException

 data TimingFailure = TimingFailure
                    deriving (Show, Typeable)

 instance Exception TimingFailure where
   toException = toException . TracerException
   fromException x = do
     TracerException a <- fromException x
     cast a

 data WriteFailure = WriteFailure
                   deriving (Show, Typeable)

 instance Exception WriteFailure where
   toException = toException . TracerException
   fromException x = do
     TracerException a <- fromException x
     cast a

Instead of writing this, one could simply write

 import Control.Exception (SomeException(SomeException))
 import Control.Exception.Hierarchical

 mkAbstractException 'SomeException "TracerException"
 mkException 'TracerException "TimingFailure"
 mkException 'TracerException "WriteFailure"

and allow Template Haskell to fill in the rest.

This libray deals with two types of exceptions: abstract and concrete exceptions. Both types can be caught with catch and other associated functions; however, only you may only extend abstract exceptions, and you may only throw concrete ones. This is a fundamental limitation of the Haskell exception hierarchy system as it currently exists.

Synopsis

Documentation

mkAbstractExceptionSource

Arguments

:: Name

the name of the super-exception’s data constructor

-> String

the name of the exception to create

-> DecsQ 

Creates an abstract sub-exception of an existing exception. As discussed in the introduction, such an exception cannot be thrown; it can only be extended.

mkExceptionSource

Arguments

:: Name

the name of the super-exception’s data constructor

-> String

the name of the exception to create

-> DecsQ 

Creates a concrete sub-exception of an existing exception. As discussed in the introduction, such an exception cannot be extended; it can only be thrown.