custom exceptions don't display line numbers

  • Thread starter opensource junkie
  • Start date
O

opensource junkie

I'm developing my first major ajax system, and in doing so I'm trying to implement a library of custom exceptions similar to ones that I've written inother languages. However, I don't think I'm doing it right, because when my custom exceptions are thrown, they don't retain any information beyond that which I manually set (specifically, I'd like to see the line of code that generated the error).

For example:

throw new Error("foo");

generates an exception with a line number that shows up in my firefox errorconsole. On the other hand, the following code:

function BadServerResponseException() {
this.name = 'BadServerResponseException';
this.message = 'The response provided by the server was invalid';
}
throw new BadServerResponseException();

only retains the information that I specifically provide it. Of course this makes perfect sense; I can hardly expect the system to read my mind and give me what I want without explicit instructions. However, I am unsure of what code will make it record the line number.

I tried a couple methods of inheritance, thinking that perhaps an exceptionthat inherits from the Error "class" would be given the necessary information. I did eventually find a method which works:

function BadServerResponseException() {
var that = new Error();
that.name = 'BadServerResponseException';
that.message = 'The response provided by the server was invalid';
return that;
}

however it records the line number of the "new Error()" call in my exceptions.js file instead of the "new BadServerResponseException()" call at the point where the error occurred.

Unfortunately I'm still in the process of understanding all the intricaciesof prototypical inheritance in javascript, so I'm wondering of someone else might be able to shed some light on the situation. Any help on this would be greatly appreciated,

-- Nate
 
D

Denis McMahon

however it records the line number of the "new Error()" call
in my exceptions.js file instead of the "new
BadServerResponseException()" call at the point where the error
occurred.

I tried:

<script type="text/javascript"> // line 1

document.write("<p>so far so good 1</p>"); // line 3

var BadServerResponseException = new Error('The response provided by the
server was invalid'); // line 5

document.write("<p>so far so good 2</p>"); // line 7

throw BadServerResponseException; // line 9

document.write("<p>so far so good 3</p>"); // line 11

</script>

and the thrown error is reported against the line that it is declared
on, not the line it is thrown on. (firefox error console)

Output:
so far so good 1

so far so good 2

Error Console:
Error: The response provided by the server was invalid
Source File: file:///.../exception.htm
Line: 5

This seems to correspond with your problem. I tried various other
things, but none of them worked.

Rgds

Denis McMahon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top