Ouananiche
Ouananiche

Reputation: 599

Add global try/catch to JavaScript

Is there some way to wrap the entire page in a try/catch, so that I can catch any error from any script that is executing?

Upvotes: 29

Views: 9032

Answers (2)

Raynos
Raynos

Reputation: 169451

Turn on the debugger and break on error.

You don't want to catch every error in the entire page on production code. That's horrible.

Upvotes: -5

Jonathon Faust
Jonathon Faust

Reputation: 12545

Use window.onerror instead of a big try/catch.

You could actually do some useful things in the error handler, like posting the error information to your server so you know when things are breaking on your page.

Upvotes: 50

Related Questions