Home » F# throwing Exception using failwith keyword

F# throwing Exception using failwith keyword

by Online Tutorials Library

F# Throwing Exception using Failwith Keyword

In F#, you can throw exceptions explicitly. You are allowed to throw custom exception. You can also throw exceptions by using predefined methods of Exception like Failwith and InvalidArgs.
Failwith keyword generates System.Exception exception. It has Failure keyword to refer exception. Let’s see an example.

Output:

Sorry, Age must be greater than 18  Rest of the code  

F# Throwing Exception Using InvalidArg Keyword

It generates System.ArgumentException. You can throw argument type exception by using InvalidArg. Let’s see an example.

Output:

System.ArgumentException: Sorry, Age must be greater than 18  
Next TopicF# Assertion

You may also like