Hello-World
Hello-World

Reputation: 9555

try catch error in javascript - get more error details

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);
  }

Upvotes: 14

Views: 26665

Answers (3)

KernelPanik
KernelPanik

Reputation: 8339

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.

Upvotes: 20

MMeersseman
MMeersseman

Reputation: 2631

Check this link out: Reference to Error.prototype

Basically you have err.name and err.message.

You also have a few vendor-specific extensions:

Microsoft => err.description and err.number.

Mozilla => err.fileName, err.lineNumber and err.stack.

Upvotes: 6

samba
samba

Reputation: 2273

function message()
{
  try
  {
  }
  catch(err)
  {
   alert(err.message); 
 }
} 

SEE HERE and HERE

Upvotes: 0

Related Questions