Programmers will definitely encounter errors when writing programs. This article is based on the concept of error handling in JavaScript. I hope it will help you when you write JavaScript programs.
Demo Demonstration
The Demo we use can be downloaded from GitHub. When the program is run, the following page will appear:
All buttons will trigger errors and throw TypeError. The following is the definition of the module:
// scripts/error.jsfunction error() {var foo = {};return foo.bar();}
An empty object foo is defined in error(), so calling foo.bar() will cause it to be undefined And report an error. Let’s use unit testing to verify:
// tests/scripts/errorTest.jsit('throws a TypeError', function () {should.throws(error, TypeError);});
We use Mocha with Should.js for unit testing.
After you clone the code base and install the dependency packages, you can use npm t to execute tests. Of course, you can also execute a test file, such as: ./node_modules/mocha/bin/mocha tests/scripts/errorTest.js
Believe me, for a dynamic language like JavaScript, no matter who It's easy to encounter errors like this.
Bad handling method
I have abstracted the event processing function corresponding to the button a little simpler, as follows:
// scripts/badHandler.jsfunction badHandler(fn) {try {return fn();} catch (e) { }return null;}
badHandler receives an fn as a callback function, the callback function is called in badHandler. We write the corresponding unit test:
// tests/scripts/badHandlerTest.jsit('returns a value without errors', function() {var fn = function() {return 1;};var result = badHandler(fn);result.should.equal(1);});it('returns a null with errors', function() {var fn = function() {throw new Error('random error');};var result = badHandler(fn);should(result).equal(null);});
You will find that if an exception occurs, badHandler simply returns null. If you match the complete code, you will find the problem:
// scripts/badHandlerDom.js(function (handler, bomb) {var badButton = document.getElementById('bad'); if (badButton) {badButton.addEventListener('click', function () {handler(bomb);console.log('Imagine, getting promoted for hiding mistakes');}); }}(badHandler, error));
If you try-catch it when an error occurs, and then just return null, I can't find what went wrong at all. This fail-silent strategy may lead to UI confusion and data confusion, and you may spend several hours debugging but ignore the code in try-catch, which is the source of the disaster. If the code is so complex that it has multiple levels of calls, it will be almost impossible to find what went wrong. Therefore, we do not recommend using the silent failure strategy, we need a more elegant approach.
Not bad but bad way
// scripts/uglyHandler.jsfunction uglyHandler(fn) {try {return fn();} catch (e) {throw new Error('a new error');}}
The way it handles errors is to catch error e and then throw a new error. This is indeed better than the previous strategy of quiet failure. If something goes wrong, I can go back layer by layer until I find the error e that was originally thrown. Simply throwing an Error ('a new error') has limited information and is not accurate. We customize the error object and send out more information:
// scripts/specifiedError.js// Create a custom errorvar SpecifiedError = function SpecifiedError(message) {this.name = 'SpecifiedError';this.message = message || '';this.stack = (new Error()).stack;}; SpecifiedError.prototype = new Error();SpecifiedError.prototype.constructor = SpecifiedError; 、// scripts/uglyHandlerImproved.jsfunction uglyHandlerImproved(fn) {try {return fn();} catch (e) {throw new SpecifiedError(e.message);}}// tests/scripts/uglyHandlerImprovedTest.jsit('returns a specified error with errors', function () {var fn = function () {throw new TypeError('type error');};should.throws(function () {uglyHandlerImproved(fn);}, SpecifiedError);});
Now , this custom error object contains the original error information and therefore becomes more useful. But because it was thrown again, it was still an unhandled error.
Interception of exceptions
One idea is to surround all functions with try...catch:
function main(bomb) {try {bomb();} catch (e) {// Handle all the error things}}
However, such code will become very Bloated, unreadable, and inefficient. Do you still remember? At the beginning of this article, we mentioned that an exception in JavaScript is just an event. Fortunately, there is a global exception event handling method (onerror).
// scripts/errorHandlerDom.jswindow.addEventListener('error', function (e) {var error = e.error;console.log(error);});
Get stack information
You can send error information to the server:
// scripts/errorAjaxHandlerDom.jswindow.addEventListener('error', function (e) {var stack = e.error.stack;var message = e.error.toString();if (stack) {message += '\n' + stack;} var xhr = new XMLHttpRequest();xhr.open('POST', '/log', true);// Fire an Ajax request with error detailsxhr.send(message);});
In order to obtain more detailed error information and save the trouble of processing data , you can also use fundebug's JavaScript monitoring plug-in to quickly access the bug monitoring service in three minutes.
The following is the error message received by the server:
If your script is placed under another domain name, if you do not enable CORS, in addition to Script error., you will not see any useful error information. If you want to know the specific solution, please refer to: Script error. Comprehensive analysis.
Asynchronous error handling
Since setTimeout is executed asynchronously, the following code exception will not be caught by try...catch:
// scripts/asyncHandler.jsfunction asyncHandler(fn) {try {// This rips the potential bomb from the current contextsetTimeout(function () {fn();}, 1);} catch (e) { }}
The try...catch statement will only capture exceptions in the current execution environment. But when the above exception is thrown, the JavaScript interpreter is no longer in try...catch, so it cannot be caught. The same goes for all Ajax requests.
We can make a slight improvement and write try...catch into the callback of the asynchronous function:
setTimeout(function () {try {fn();} catch (e) {// Handle this async error}}, 1);
However, such a routine will cause the project to It is full of try...catch, and the code is not concise. Also, the V8 engine that executes JavaScript discourages the use of try...catch in functions. Fortunately, we don't need to do this, the global error handling onerror will catch these errors.
in conclusion
My advice is don't hide errors, be brave enough to throw them out. No one should be ashamed because a bug in the code causes the program to crash. We can interrupt the program and let the user start over. Mistakes are inevitable, how you deal with them is what matters.
The above content is how to deal with JavaScript errors. If you find it useful, please quickly collect it.
Related recommendations:
Perfect solution to PHP installation Implementation steps and error reporting of extending mysqli
MySQL database error: Solution to Too many connections
The above is the detailed content of How to handle JavaScript exceptions. For more information, please follow other related articles on the PHP Chinese website!

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools