Using KeyNotFoundException and exception patterns - vilinski/nemerle GitHub Wiki

Using KeyNotFoundException and exception patterns

  • Category: Exceptions
  • Description: Raising a 'KeyNotFoundException' exception, and using exception patterns
  • Code:
using Nemerle;
using System;
using System.Console;
using System.IO;
using System.Collections.Generic;

    try 
    {
        WriteLine("About to raise an exception...");
        match (DateTime.Now.DayOfWeek)
        {
         | DayOfWeek.Monday => throw KeyNotFoundException() 
         | _                => assert(false, "it's not Monday")
        }
    }
    catch
    {
        | _ is KeyNotFoundException => WriteLine("Caught a 'KeyNotFoundException' exception, it must be Monday")
        | x is AssertionException   => WriteLine($"Caught a 'AssertionException' exception: $x")
    }
  • Execution Result (in case of Monday):
About to raise an exception...
Caught a 'KeyNotFoundException' exception, it must be Monday
  • Execution Result (in other cases):
About to raise an exception...
Caught a 'AssertionException' exception: Nemerle.Core.AssertionException: assertion failed in file C:\Users\Alexander\AppData\Local\Temp\tmp373B.tmp, line 13: it's not Monday
   в tmp373B.Main()

[Copyright ©](Terms of use, legal notice)