Skip to content

Grok Exceptions

Alex Zimin edited this page Jul 11, 2011 · 3 revisions

This page is a part of the Grokking Nemerle tutorial.

In Nemerle you can use exceptions in a similar way you would use them in C#. The only difference is the syntax for the catch handlers -- it looks more or less like matching. You can even use the _ to denote any exception that has to be caught.

some_function (foo : string) : void
{
  when (foo == null)
    throw System.ArgumentException ("foo");
  // do something
}

some_other_function () : void
{
  try {
    some_function ()
  } 
  catch {
    | e is System.ArgumentException =>
      printf ("a problem:\n%s\n", e.Message)
  } 
  finally {
    printf ("yikes")
  }
}

some_other_function_2 () : void
{
  try {
    some_function ()
  } catch {
    | e is System.ArgumentException =>
      printf ("invalid argument\n")
    | _ =>
    // just like:
    // | _ is System.Exception =>
      printf ("a problem\n")
  }
}

some_other_function_3 () : void
{
  def f = open_file ();
  try {
    some_function ()
  } 
  finally {
    f.close ()
  }
}

class MyException : System.Exception {
  public this () {}
}
Clone this wiki locally