[Switch] JavaScript Error (Error) and Exception (Exception) Handling

PHP rarely uses error handling, because the framework has helped a lot, so basically no PHP errors have been taken over. PHP is a back-end dynamic processing language and has little to do with users, so users don't care if an error occurs. But JavaScript is very different. The error of js is very obvious. People with a little advantage of network foundation will quickly open F12 to check the error. Recently, I have made a project that requires a lot of js, and a large number of native js, jq framework and other plug-ins are mixed together. , so there will inevitably be some inexplicable problems. Sometimes when I look at the full red error report, I wonder which user will open it and see it? After searching for information (rhinoceros book), I finally found the mechanism of JavaScript error handling, which is the solution.


First, the difference between JavaScript error (Error) and exception (Exception).

Here carefully distinguish the difference between Error and Exception. Looking for information on the Internet, most of the information found are Java-related information, and the errors and exceptions of JS can also be obtained by comparing the beginning of JS.

Error (error) represents system-level errors and exceptions that the program does not have to handle. It is an internal error or hardware problem in the java operating environment, such as insufficient memory resources, etc. For this kind of error, the program is basically powerless, except to quit running. choose. Exception (violation) represents an exception that needs to be caught or needs to be handled by the program. It deals with problems caused by flaws in program design or general problems caused by external input, etc., and the program must deal with it.

In a nutshell: Error means that an error occurs when the code is compiled, and the code cannot be compiled, such as a syntax error, and the error must be corrected before it can be recompiled. The code cannot skip errors.

                 Exception is when the code runs again, an error occurs, such as a property in the object does not exist, or the data type is wrong. The code can continue to execute, but an error will be output to the console to alert the programmer.

 

The error handling we are talking about basically refers to the second case of Exception, which is captured by try catch or onerror.

 

Second, how JavaScript errors are output to the console - the principle of JavaScript error handling.

When an exception is thrown, the JavaScript interpreter will immediately stop the currently executing logic and jump to the nearest exception handler (refer to the related concepts of bubbling and variable scope. A simple sentence: the son made a mistake, Look for Lao Tzu, and look up layer by layer until you find someone who can bear mistakes). If no error handler is found on all lexical structures and call stacks. JavaScript will treat exceptions as program errors and report them to the user.

 

3. Use try catch finally handler

The syntax is as follows

copy code
try {
     // code that may go wrong here 
}
 catch (e)
{
    // Execute here after code error. 
}
 finally
{
    // This will be executed regardless of whether the code reports an error or not. 
}
copy code

 

3. Use window.onerror

copy code
window.onerror    =   dieError;
function dieError(msg,url,line){
    var txt   =   "ERROR: "+msg+"\n";
        txt  +=   "URL: "+url+"\n";
        txt  +=   "LINE: "+line+"\n";
        alert(txt);
        return true;
}   
copy code

Personally, I feel that this Window.onerror is not very useful. It may be because of an earlier reason, and there is already a better processing method to replace it.

Source: https://www.cnblogs.com/roverliang/p/5070522.html

you may be interested in

  1. Detailed explanation of 10 major JavaScript errors from 1000+ project data analysis
  2. 10 Advanced Tips for Debugging with Console
  3. Fundebug supports distinguishing Source Map versions
  4. Debug front-end HTML/CSS

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325603284&siteId=291194637