JavaScript Errors Properties & Throwing Exceptions

Quick bit of information on JavaScript errors and throwing errors. (This post uses error and exception as synonyms… not sure what the correct term is in JavaScript)

I was trying to figure out how to get details from an exception thrown in JavaScript and it seems the structure is like this:

{
  message : “the error message”,
  fileName : “file:///c:/User/me/Desktop/Stuff.js”,
  lineNumber : 57,
  stack : “bunch of stuff here”
}

(Used json2.js to stringify the error message as Firebug didn’t seem to be showing it… ?)

When you throw your own exceptions you can of course throw anything including a string like this:

throw “This is a string”;

… and when you analyse what you caught it will be a simple string with that value.

try {
    throw "This is a string";
} catch(error) {
    alert(error);
}

The result will be an alert box with the value "This is a string" displayed.

Just found out here that you can do this:

throw new Error(reason);

Advertisements

2 Responses to “JavaScript Errors Properties & Throwing Exceptions”

  1. 70-480 Exam Preparation Guide | The SharePoint Viking Says:

    […] and respond to error codes Throw an exception Request for null checks Implement try-catch-finally […]

  2. Exam 70-480 Programming with HTML5, Javascript and CSS3 Study Guide | The SharePoint Viking Says:

    […] and respond to error codes Throw an exception Request for null checks Implement try-catch-finally […]


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: