3

This question divides to 2 really :

  • Can i catch an exception with js without using try catch? doesn't have to "catch" it as in to continue executing the code but to be notified that an exception as happend.

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

  • Can i wrap code with try/catch during run time?
eric.itzhak
  • 15,752
  • 26
  • 89
  • 142
  • i wanna catch it with javascript – eric.itzhak Nov 27 '12 at 12:37
  • Why do you need to do this? There is probably a better way to solve your actual problem. – hugomg Nov 27 '12 at 12:41
  • I can not touch the actuall code only add to it so i can't think of a different solution besides these 2 – eric.itzhak Nov 27 '12 at 12:42
  • Wrap the code you can't touch with a try catch – Asad Saeeduddin Nov 27 '12 at 12:45
  • 1
    i can't do that either, only in runtime if it's possible =/ – eric.itzhak Nov 27 '12 at 12:48
  • try/catch is primarily intended (in one way or another) for client code, not the function definitions. So you should be adding it somewhere around/above where the function is being invoked (rather than modifying the function itself). It certainly sounds like you should have access to do that. – Matt Whipple Nov 27 '12 at 12:51
  • i have access for that but i need to find a solution without using it unless i am able to add it in runtime. – eric.itzhak Nov 27 '12 at 12:52
  • You could add it whenever you want. I'm not sure what you mean by "runtime": function invocation is runtime. There's pretty much no other time in JavaScript. If you want to add it dynamically you could use a higher order function to wrap an existing function, but there's likely no practical benefit to that. – Matt Whipple Nov 27 '12 at 12:54
  • http://stackoverflow.com/questions/951791/javascript-global-error-handling – Matt Whipple Nov 27 '12 at 12:55
  • hmm for the example let's say you want to include an external js file to catch errors on another external js file, that what i call "runtime" – eric.itzhak Nov 27 '12 at 12:56
  • That's not what runtime is, you should have been more specific. Fucntions are first class citizens in JS, once a function is defined you can easily modify it by other code in the same namespace such as wrapping it in a try/catch. – Matt Whipple Nov 27 '12 at 12:59
  • can you give me an example of how i do that? – eric.itzhak Nov 27 '12 at 13:00

2 Answers2

1

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.

Community
  • 1
  • 1
Matt Whipple
  • 7,034
  • 1
  • 23
  • 34
  • This + the link to the question you commented me anout gives a perfect answer, please edit your question with the link to that question. – eric.itzhak Nov 27 '12 at 13:05
0

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.

Javadotnet
  • 125
  • 1
  • 1
  • 6