|
throw() throws an exception. When a String object, message, is passed RuntimeException is thrown.
e.g.
throw(IOException()) throw("Hello")
|
catch() defines an exception handler. When one of the defined exception is thrown, the corresponding handler is executed and the function immediately returns the result of the handler.
The definition of the exception handler is saved in an internal registry on the current stack frame. When an exception is thrown the class of the exception or one of the superclasses is searched from the internal registry.
An exception Handler is just a function with one parameter. An exception handler is called with the Exception object as the parameter. If no handler is registered for the exception thrown, an error message is printed at the top of the interpreter loop.e.g. (Java)
try { task1(); task2(); task3(); } catch(IOException e){ return "error"; }
try-catch block in Java can be written as follows in Pnuts.
e.g. (Pnuts)
(function (){ catch(IOException, function (e) "error") task1() task2() task3() })()
|
To catch syntax errors, specify pnuts.lang.ParseException class to the first parameter of catch() function.
Pnuts API Overview describes how to handle an exception in Java.