code). end; Erlang try % some dangerous code catch throw:{someError, X} -> ok; % handle an exception error:X -> ok; % handle another exception _:_ -> ok % handle all exceptions after % clean up end F# In addition to the OCaml-based try...with, F# also has the separate try...finally construct, which has the same behavior as a try block with a finally clause in other .NET languages. For comparison, this is a translation of the C# sample above. try try () (* Code that could throw an exception. *) with | :? System.Net.WebException as ex -> () (* Handles a WebException. The exception object is stored in "ex". *) | :? exn -> () (* Handles any CLR exception. Since the exception has not been given an identifier, it cannot be referenced. *) | _ -> () (* Handles anything that might be thrown, including non-CLR exceptions. *) finally () (* Always run when leaving the try block, regardless of whether any exceptions were thrown or whether they were handled. Often used to clean up and close resources such a file handles. May not be run when Environment.FailFast() is called and in other system-wide exceptional conditions (e.g. power loss), or when the process crashes due to an exception in another thread. *) For comparison, this is translation of the OCaml sample below. exception MyException of string * int (* exceptions can carry a value *) let _ = try raise (MyException ("not enough food", 2)); printfn "Not reached" with | MyException (s, i) -> printf "MyException: %s, %d\n" s i | e -> (* catch all exceptions *) eprintf "Unexpected exception : %O" e; eprintf "%O" e.StackTrace Haskell Haskell does not have special syntax for exceptions. Instead, a ///. interface is provided by functions. import Prelude hiding(catch) import Control.Exception instance Exception Int instance Exception Double main = do catch (catch (throw (42::Int)) (\e-> print (0,e::Double))) (\e-> print (1,e::Int)) prints (1,42) in analogy with this C++ #include <iostream> using namespace std; int main() { try {throw (int)42;} catch(double e) {cout << "(0," << e << ")" << endl;} catch(int e) {cout << "(1," << e << ")" << endl;} } Another example is do { -- Statements in which errors might be thrown } `catch` \ex -> do { -- Statements that execute in the event of an exception, with 'ex' bound to the exception } In purely functional code, if