SML# Document Version 4.0.0
19 Expressions

19.14 Exception handling expression ⟨exp⟩ handle ⟨match⟩

This expression catches an exception raised during evaluation of expression ⟨exp⟩. ⟨match⟩ is of the form

  ⟨pat1⟩ => ⟨exp1⟩
| ⋯
| ⟨
patn⟩ => ⟨expn⟩

that indicates the sequence of pairs of a exception pattern and expression to be evaluated if the raised exception is matched with the pattern.

Each pattern ⟨pati⟩ must includes an exception constructor and therefore be of exception type exn. The type of every expression ⟨expi⟩ must be identical to that of ⟨exp⟩. This expression is evaluated as follows.

  • •

    ⟨exp⟩ is evaluated at first. If this evaluation is normally finished, its value is the value of this entire expression.

  • •

    If an exception is raised during the evaluation, it tries to match the exception with each pattern from ⟨pat1⟩ to ⟨patn⟩. If it a match succeeds, it binds variables in ⟨pat⟩i to corresponding parameters of the exception object and evaluates ⟨expi⟩. The value of ⟨expi⟩ is the value of this entire expression.

  • •

    If the exception does not match with any pattern, the exception is propagated to the outer expression surrounding this expression.