[nem-en] Re: Exception matching

Andrey Khropov andrey.khropov at gmail.com
Sun Jan 28 20:03:20 CET 2007


Vladimir Reshetnikov 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?

I think because in .NET in general AFAIK there're no guarantees on possible
exceptions that could be thrown from inside the try block (because there's no
notion of function exception specification unlike in Java/C++) so you always
match against the most generic type System.Exception.

So I guess you should do like that:

catch
{
	| e is ProgramException => match(e)
		{
		     | Failure => WriteLine("Oops!");
		     | Warning(severity) when severity > 10
		         	    => WriteLine("Something is going wrong");
		     | _ => ()
		}
}


-- 
AKhropov




More information about the devel-en mailing list