0

For the JavaScript code like this:

try { MyJob.process(); } catch(e) { console.log("Exception occur!"); } 

I run the code in Chrome or FireFox, When the exception happens, the line number of "Exception occur!" will be shown in console, but the original exception in MyJob won't be there. Is there any solution that show the original position where the exception happens and keep the try-catch that I write here?

2 Answers 2

2
window.onerror = function ( msg, url, num ) { alert ( "Error: " + msg + "\nURL: " + url + "\nLine: " + num ); return true; }; 

This will show most of the errors.

In the catch block add:

catch(e) { console.log( e.name + ": " + e.message ); } 

More about the error handling at JavaScriptkit

If the try/catch block is inside a function you could take advantage of the arguments.callee mdn msdn

function foo() { try { someFunction(); } catch (e) { var f = arguments.callee.toString().substr ( "function ".length); f = f.substr(0, f.indexOf('(')); alert ( "Error type : " + e.name + "\nError : " + e.message + "\nIn function : " + f ); } } 

Result will be:

Error type : ReferenceError Error : someFunction is not defined In function : foo 
Sign up to request clarification or add additional context in comments.

Comments

0

Temporary comment out the try and catch, then step through with the Chrome (or Firefox) JavaScript debugging tools.

//try { MyJob.process(); /*} catch(e) { console.log("Exception occur!"); }*/ 

After identifying the issue, remove the comments to restore the original error handling.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.