This project is read-only.
Raising a 'Failure' exception

Category:
Exceptions
Description: How to raise a simple exception
Code:
let ExceptionSample1() : unit =
failwith "Here's how to raise a simple 'Failure' exception"


Execution Result:
Exception raised: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Exception: Here's how to raise a simple 'Failure' exception
at Microsoft.FSharp.Core.Operators.FailWith[T](String message)
at Samples.Beginners.ExceptionSample1() in C:\MyCode\Samples\SampleProject\SampleProject\Samples.Beginners.fs:line 148
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at Support.Helper.Utils.samples@190-3.Invoke(Unit unitVar0) in C:\MyCode\Samples\SampleProject\SampleProject\Supportive.Helper.fs:line 190
at Support.Helper.Utils.getExecutionResult(FSharpOption`1 sampleOption) in C:\MyCode\Samples\SampleProject\SampleProject\Supportive.Helper.fs:line 256

Raising and catching a 'Failure' exception

Category:
Exceptions
Description: How to raise and catch a 'Failure' exception
Code:
let ExceptionSample2() =
try
printfn "About to raise a simple 'Failure' exception..."
failwith "Whoa!"
with
Failure msg ->
printfn "Caught a simple 'Failure' exception, msg = '%s'" msg

Execution Result:
About to raise a simple 'Failure' exception...
Caught a simple 'Failure' exception, msg = 'Whoa!'

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

Using ArgumentException and exception patterns

Category:
Exceptions
Description: Raising an 'ArgumentException' exception, and filtering for .NET exceptions
Code:
let ExceptionSample4() =
try
printfn "About to raise an ArgumentException exception..."
if DateTime.Now.DayOfWeek = DayOfWeek.Tuesday then
raise (new System.ArgumentException("Not today, it's Tuesday"))
else
raise (new System.ApplicationException("Hey, it's not Tuesday..."))
with
| :? System.ArgumentException as e ->
printfn "Caught an ArgumentException, e.Message = %s" e.Message
| :? System.ApplicationException as e ->
printfn "Caught an ApplicationException, e.Message = %s" e.Message
| _ ->
printfn "Some other exception was caught"

//--------------------------------------------------------

Execution Result:
About to raise an ArgumentException exception...
Caught an ApplicationException, e.Message = Hey, it's not Tuesday...

Last edited Sep 13, 2011 at 12:24 AM by ttliu2000, version 1

Comments

No comments yet.