The code works by calling a function or assigning a value inside the block try-catch
:
module public test
open System
let testing =
let divide = fun a b -> a / b
try
divide 100 0
with
| :? System.DivideByZeroException as ex -> printfn "Exception! %s (ex.Message); None.Value
either
module public test
open System
let testing =
let divide = fun a b -> a / b
let mutable num = divide 100 10
try
num = divide 100 0
with
| :? System.DivideByZeroException as ex -> printfn "Exception! %s (ex.Message); None.Value
Declaring a name inside the block doesn't let me:
module public test
open System
let testing =
/*
El bloque que sigue a este objeto 'let' está sin finalizar. Cada bloque de
código es una expresión y debe tener un resultado. 'let' no puede ser el
elemento de código final en un bloque. Considere la posibilidad de asignar a
este bloque un resultado explícito.
*/
let divide = fun a b -> a / b
try
let num = 100
let num2 = 20
divide num num2
with
| :? System.DivideByZeroException as ex -> printfn "Exception! %s (ex.Message); None.Value
This is because inside the blocks
try-catch
it is not possible to declare variables. This is so to prevent them from declaring new variables because an error has been thrown somewhere in the blocktry
, which would lead to the execution of the blockcatch
without declaring the other variables. Therefore, you must declare the variables outside of the blocks,try-catch
and you will be able to assign values to them inside the blocktry-catch
.This should solve your problem.