1

I have been quite curious about how javascript reacts to errors(like ReferenceError, for example). When it encounters a runtime error, it seems to return from the function that it is called in which, in turn, fails the function it was called in.

Does it consequently fail all the functions in the frame stack? (This is more of a question out of academic curiosity. Hope somebody can explain it to me?)

Thanks!

1
  • This chapter from Eloquent JavaScript might interest you: Error Handling Commented Oct 14, 2011 at 4:36

3 Answers 3

2

JavaScript exception handling is much the same as other languages' error handling - it will throw an error up the call stack until handled by the catch of a try block. If there is no try/catch, then the current execution will stop.

All the function calls below the catch will be exited - they won't return anything, and the following lines of code will not be executed.

Sign up to request clarification or add additional context in comments.

1 Comment

You can use window.onerror event to get a handle on any error which is not caught. (Note: I am not sure about browsers other than IE)
1

It continues up the call stack until it reaches a a try...catch block.

You can see a simple example on JSFiddle.

1 Comment

Yes and it ultimately fails if a try catch block isn't found. And it fails silently if you don't have a debugger.
0

It returns an Object of the error, and like most Objects, it has has methods and properties

try {
    a/1;
} catch(err) {
    console.log(err.name) //ReferenceError
    console.log(err.message); //a is not defined
    console.log(err.constructor); //ReferenceError() { [native code] }
    console.log(err.toString()); //ReferenceError: a is not defined
    console.log(err.stack) 
    /*
      ReferenceError: a is not defined
      at errCatch (<anonymous>:3:11)
      at <anonymous>:1:11
    */
}

note, console.log(Object) is the same as Object.toString(). Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.