Home » F# Exceptions Handling

F# Exceptions Handling

by Online Tutorials Library

F# Exception handling

Exception handling is a standard mechanism to handle abnormal termination of program. Exception is a situation that occurs during program execution. It may lead to terminate program abnormally like dividebyzero or nullpointer.

F# provides built in set of tools to handle exception.

F# Exception types

F# provides two categories of exception:

  1. .Net type Exception
  2. F# exception type

Here, we will discuss only about F# exception.

F# Example without try/with

In the above code ExExample, function returns integer after dividing. It works fine if denominator is greater than 0. If denominator is zero, it will throw DevideByZero Exception and terminate program abnormally.

Output:

System.DivideByZeroException: Attempted to divide by zero.  Stopped due to error.  

To handle this situation, F# provides try-with block. It helps to put suspected code inside try block. Exception handler with catch exception object. It provides information about the exception.

Now let’s see how to handle this situation. See next topic in details.

You may also like