Panic.catch
Panic.catchpanic_typeactionhandler
Documentation
Executes the provided action and if a panic matching the provided type was
thrown, calls the provided callback.
If action executes successfully, the result of Panic.catch is the result
of that action. Otherwise, if a matching panic is thrown from within the
action, the result is obtained by calling the provided handler callback.
Any non-matching panics are forwarded without changes.
Arguments
panic_type: The expected panic type. It can either be an Enso type or a Java class.action: The code to execute that potentially panics.handler: The callback to handle the panics. The callback will be provided with aCaught_Panicinstance encapsulating thepayloadof the caught panic and its stacktrace.
Examples
Handling a specific type of panic.
Panic.catch Illegal_Argument (Panic.throw (Illegal_Argument.Error "Oh no!" Nothing)) error->
"Caught an `Illegal_Argument`: "+error.payload.message
Handling any panic.
Panic.catch Any (Panic.throw (Illegal_Argument.Error "Oh no!" Nothing)) error->
"Caught some panic!"
Convert a string to an integer, catching the Java NumberFormatException
and converting it to a more Enso-friendly dataflow error.
polyglot java import java.lang.Long
polyglot java import java.lang.NumberFormatException
parse str =
Panic.catch NumberFormatException (Long.parseLong str) caught_panic->
Error.throw (Illegal_Argument.Error "The provided string is not a valid number: "+caught_panic.payload.getMessage)