About javascript try catch

Asked 1 years ago, Updated 1 years ago, 29 views

What I'm trying to do right now is to ajax the error that entered the catch with try catch and send an email.

However, even if you try to catch the outline of the javascript code, it will not be caught.
However, if you try to catch the code with the error, it will catch it.

Note: 20200903
I was able to confirm that I can't get a try catch in a general frame, so I can now catch it once I have everything in each function.
I'm not sure if it's all taken...(The reason is that there is a function that puts variables in the function, and in that case, I think it's possible to take it...)

javascript

2022-09-30 19:51

2 Answers

(There is a lack of information, so the answer may be wrong.)

try{...}catch catches exceptions that occurred in code that ran from the beginning to the end of the blocks surrounded by them.Exceptions to functions defined in this block but not running are not covered.

function foo(){
  // Exceptions from this function can be caught in the catch below.
}

try{
  foo();
  document.addEventListener("load", function(e){
    // This function does not run until catch.
    // Exceptions cannot be caught in the catch below.
  });
} catch(e){
  // …
}

You may be able to use the error event for logging purposes.

window.addEventListener("error", function(e){
  // e.error is the exception
});


2022-09-30 19:51

Even if you try to catch the javascript code, it will not catch it.
However, if you try to catch the code with the error, it will catch it.
Is there something wrong?

Correct!

Trycatch is a way to take some remedial action (e.g., detailing the errors and exceptions that occurred (helpful for debugging) or taking some action (e.g., closing all files used to prevent data loss) rather than abandoning "An exception has occurred."


2022-09-30 19:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.