How can I get more error details from a javascript catch?
Are there more parameters to get more details from the caught error.
try {
var s = null;
var t = s.toString();
} catch(err) {
alert(err);
}
The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
The try... catch construct allows to handle runtime errors. It literally allows to “try” running the code and “catch” errors that may occur in it.
The try-catch statement should be used any time you want to hide errors from the user, or any time you want to produce custom errors for your users' benefit. If you haven't figured it out yet, when you execute a try-catch statement, the browser's usual error handling mechanism will be disabled.
The Error Object has several properties that you can use. One property you can use to get the message of the error, is .message
, as in:
catch(err) {
alert(err.message);
}
The .name
property returns the type of error as in:
catch(err) {
x = err.name;
// ... do something based on value of x
}
The name describes the type of error, and the value of .name
can be : EvalError, RangeError, ReferenceError, SyntaxError, TypeError
, and URIError
. You may decide to handle the error differently depending on the error type which is returned by the .name
property.
A good tutorial can be found on JavaScriptKit. The is also an article on the error object at Mozilla Developer Network.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With