Method Function.setErrorHandler
Set a error handler for the function.
-
Syntax
Function Function.setErrorHandler(Function f , [ Number v ])
When called this method will wrap the caller function in a try/catch block and return this as a new function. If an error is subsequently thrown by the function, execution will be passed to the error handlerfwhich is provided as the first argument.
The error handler itself is passed two parameters - the function which caused the error and the error object which was thrown.
If an error occurs the return value from the error handler is passed as the return value of the calling function. -
Parameters
Function f- The handler which should execute if the function throws an error.
Number v(Optional)- This parameter sets the verbosity level when the error handler is used with the JSLab error object.
- Return value A new function wrapped in a try/catch block
-
Examples
- Setting an error handler for a given function.
// Create function with intentional error function myFunc(s) { alert(s); notDeclared } // Create error handler function with 2 parameters f and e // f is the function throwing the error // e is the error object function eHandler(f,e) { // Error handling code goes here alert('Now fixing error'); } // Now we assign the error handler myFunc = myFunc.setErrorHandler(eHandler); // Try to execute myFunc myFunc('Hello world'); // Output is 'Hello World' followed by 'Now fixing error'
- Related Function.removeErrorHandler
- Resources You can use this function along with the JSLab Error object and the JSLab XHR object for an easy way to report client side errors to a server for logging. See the article Auto reporting JavaScript errors for more information.
-
Download
Source
Unless otherwise noted all code in the JSLab Standard Library is licensed as GPLv3. See http://www.gnu.org/licenses/gpl.html for the entire license.
