[nem-en] Exception matching

Michal Moskal michal.moskal at gmail.com
Sun Jan 28 19:54:13 CET 2007


On 1/28/07, Vladimir Reshetnikov <v.reshetnikov at gmail.com> wrote:
> Can I do something like this?
>
> using System;
> using System.Console;
>
> variant ProgramException : Exception {
>     | Failure
>     | Warning {
>         severity : int
>     }
> }
>
> try {
>     throw ProgramException.Failure();
> }
> catch {
>     | ProgramException.Failure /* error : exception catch pattern must be in
> form of `| e is ExceptionType => handler' or`| e => handler' for
> System.Exception */
>     | ProgramException.Warning(severity) when severity > 10
>         => WriteLine("Oops");
> }
>
> Why currently ncc does not allow such patterns in catch block?

The problem is that try-catch blocks are translated to the .try/.catch
in MSIL. In those blocks one can check if exception is of given type
(this is the 'is' check in pattern matching), and also execute any
other testing code. The later feature doesn't work on mono though.

So while it appears as a pattern matching, it's really only a limited
form of it.

This can be fixed by translating:

try E catch | p1 => e1 ... | pN => eN

into:

try E catch { e is exception when
  mutable result;
  match (e) {
    |p1 => result = e1; true
    ....
    |pN => result = eN; true
    | _ => false
  } => result
}

but a/ it wouldn't work on mono, b/ it should not be applied when the
actuall matching code is a simple series of 'is' tests (the common
case).

-- 
   Michał


More information about the devel-en mailing list