4

If an unhandled exception occur in javascript code all browsers create beauty message in its log including exception type, message and stack trace and/or useful link to the source line. Now I want to catch an exception, put the same log as unhandled exception, and continue execution. I tried to write something like

try {
...
} catch (e) {
console.log(e);
}

but all I got in Chrome is the exception type name in the log without any description and stack or source line link. How to create beautiful exception log message same as unhadled exception but for caught exception and continue execution?

Raidri
  • 17,258
  • 9
  • 62
  • 65
Alexander Pravdin
  • 4,982
  • 3
  • 27
  • 30
  • Possible duplicate of [How to log exceptions in JavaScript](http://stackoverflow.com/questions/1238000/how-to-log-exceptions-in-javascript) – Michael Freidgeim Mar 08 '16 at 07:21

1 Answers1

4

You can use the stack property of the Error object to get a stack trace.

If you're not throwing the exception yourself, you could wrap it in an error obect to provide a trace to the catch block at least.

try{
  throw new Error("Test");

}
catch(e){
  console.log(e.stack);
}
Ben McCormick
  • 25,260
  • 12
  • 52
  • 71