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