a true value" end end function attempt(arg) success, value = pcall(foo, arg) if not success then print("Error: " .. tostring(value)) else print("Returned: " .. tostring(value)) end end attempt("hello") -- Returned: hello attempt(nil) -- Error: stdin:5: Not a true value attempt({}) -- Returned: table: 00809308 if foo(42) then print "Success" end -- Success Anonymous function if pcall( function() -- Do something that might throw an error. end) then print "No errors" -- Executed if the protected call was successful. else print "Error encountered" -- Executed if the protected call failed. end print "Done" -- Will always be executed Next Generation Shell Defining custom exception type type MyError(Error) Raising exceptions throw MyError("this happened") Exception handling and propagation try { # something } catch(e:MyError) { guard e.val = 7 # ... } catch(e:MyError) { # ... } catch(e:Error) { # ... } Ignoring exceptions - try without catch try 1/0 # evaluates to null Ignoring exceptions - "tor" operator "tor" is try-or operator. In case of any exception when evaluating the argument on the left, evaluates to the argument on the right. 1/0 tor 20 # evaluates to 20 "block" - facility to use exceptions to return a value my_result = block my_block { # "block" catches exception thrown by return below # do calculation if calculation_finished() { my_block.return(42) # throws exception } } Objective-C Exception declarations NSException *exception = [NSException exceptionWithName:@"myException" reason:@"yourReason" userInfo:nil]; Raising exceptions @throw exception; Exception handling and propagation @try { ... } @catch (SomeException *se) { // Handle a specific exception type. ... } @catch (NSException *ne) { // Handle general exceptions. ... // Propagate the exception so that it's handled at a higher level. @throw; } @catch (id ue) { // Catch all thrown objects. ... } @finally { // Perform cleanup, whether an exception occurred or not. ... } OCaml exception MyException of string * int (* exceptions can carry a value *) let _ = try raise (MyException ("not enough food", 2)); print_endline "Not reached" with | MyException (s, i) -> Printf.printf "MyException: %s, %d\n" s i | e -> (* catch all exceptions *) Printf.eprintf "Unexpected exception : %s" (Printexc.to_string e); (*If using Ocaml >= 3.11, it is possible to also print a backtrace: *) Printexc.print_backtrace stderr; (* Needs to beforehand enable backtrace recording with Printexc.record_backtrace true or by setting the environment variable OCAMLRUNPARAM="b1"*) Perl 5 The Perl mechanism for exception handling uses to throw