Google News
logo
F# - Interview Questions
What is FailWith and InvalidArg in F#?
In F#, you can throw exceptions explicitly. You are allowed to throw a custom exception. You can also throw exceptions by using predefined methods of Exception like Failwith and InvalidArgs.
 
Example of FailWith keyword : 
let TestAgeValidation age  =  
  try  
     if (age<18) then failwith "Sorry, Age must be greater than 18"  
  with  
     | Failure(e) -> printfn "%s" e;   
  printf "Rest of the code"  
TestAgeValidation 10  
Example of InvalidArg keyword
let TestInvalidArgument age =  
 if(age<18) then  
  invalidArg "TestInvalidArgument" ("Sorry, Age must be greater than 18")  
  
TestInvalidArgument 10
Advertisement