eric.itzhak
eric.itzhak

Reputation: 16072

How to catch errors without try/catch OR wrap code with try/catch in runtime?

This question divides to 2 really :

Meaning i just want to be notified about exception no matter how thw browser reacts afterwards.

Upvotes: 3

Views: 1096

Answers (2)

Javadotnet
Javadotnet

Reputation: 125

You can override onerror method in javascript. Try that to get some code to be executed which may be to notify you that error has been occured and rest of lines will continue execution.

in your js function add following lines to override onerror method.

window.onerror = function() {
alert("you have got an error");
};

Hope it helps.

Upvotes: 0

Matt Whipple
Matt Whipple

Reputation: 7134

Somewhere in your JS:

window.someDefinedFunction = function() {
  //Old risky function
}

Anywhere else where that function could be called:

window.someDefinedFunctionUnsafe = window.someDefinedFunction;
widnow.someDefinedFunction = function() {
  try {
    window.someDefinedFunctionUnsafe();
  }
  catch {
    //React to error
  }
}

Javascript global error handling is a link to a brute force approach. You may want to swap the onerror only for the duration of the risky call depending on what you're looking to do.

Upvotes: 1

Related Questions