Tuesday, November 30, 2010

Studying F# : catch the exception

In ruby, the clauses for catching some exception is like below:
begin
  # some actions
rescue
  # the action when error occurs
else
  # the action when error does not occur
ensure
  # the final action whether error occurs or not
end

But, in F#, there are two point that is different from ruby.

  1. There are no clause corresponding else.
  2. There are only clauses either try/with(corresponding begin/rescue) or try/finally(corresponding begin/ensure)

In other words, clause try/with/finally is Forbidden.

> let results =    
-     try
-         (1 / 0)
-     with
-         | ex -> "Some error occurs."
-     finally
-         "Final";;

      finally
  ----^^^^^^^

/Users/youhei/stdin(9,5): error FS0010: Unexpected keyword 'finally' in binding. Expected incomplete structured construct at or before this point or other token.

No comments:

Post a Comment