Using KeyNotFoundException and exception patterns

Category:
Exceptions
Description: Raising a 'KeyNotFoundException' exception, and using exception patterns
Code:
let ExceptionSample3() =
try
printfn "About to raise an exception..."
match DateTime.Now.DayOfWeek with
| DayOfWeek.Monday -> raise(KeyNotFoundException())
| _ -> failwith "it's not Monday"
with
| :? KeyNotFoundException ->
printfn "Caught a 'KeyNotFoundException' exception, it must be Monday"
| Failure msg->
printfn "Caught a 'Failure' exception: %s" msg

Execution Result:
About to raise an exception...
Caught a 'KeyNotFoundException' exception, it must be Monday

Last edited Sep 14, 2011 at 2:43 AM by ttliu2000, version 1

Comments

No comments yet.