Làm cách nào để đưa ra một lỗi mới trong JavaScript?

Tóm lược. trong hướng dẫn này, bạn sẽ học cách sử dụng câu lệnh JavaScript

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
1 để đưa ra một ngoại lệ

Giới thiệu về câu lệnh ném JavaScript

Câu lệnh

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
1 cho phép bạn đưa ra một ngoại lệ. Đây là cú pháp của câu lệnh

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
1

throw expression;

Code language: JavaScript (javascript)

Trong cú pháp này,

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
4 chỉ định giá trị của ngoại lệ. Thông thường, bạn sẽ sử dụng một thể hiện mới của lớp

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
5 hoặc các lớp con của nó

Khi gặp câu lệnh

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
1, công cụ JavaScript ngừng thực thi và chuyển điều khiển tới khối

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
7 đầu tiên trong ngăn xếp cuộc gọi. Nếu không có khối

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
8 nào tồn tại, công cụ JavaScript sẽ chấm dứt tập lệnh

Ví dụ về ngoại lệ ném JavaScript

Hãy lấy một số ví dụ về việc sử dụng câu lệnh

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
1

1) Sử dụng câu lệnh ném JavaScript để ném ngoại lệ

Ví dụ sau sử dụng câu lệnh

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
1 để đưa ra một ngoại lệ trong một hàm

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)

Làm thế nào nó hoạt động

Trước tiên, hãy xác định hàm

Uncaught The first argument must be a number

1 chấp nhận hai đối số và trả về tổng của chúng. Hàm

Uncaught The first argument must be a number

1 sử dụng toán tử

Uncaught The first argument must be a number

3 để kiểm tra loại của từng đối số và đưa ra một ngoại lệ nếu loại không phải là số

Thứ hai, gọi hàm

Uncaught The first argument must be a number

1 và truyền một chuỗi và một số vào đó

Thứ ba, hiển thị kết quả cho bàn điều khiển

Tập lệnh gây ra lỗi vì đối số đầu tiên (_______12_______5) không phải là số

Uncaught The first argument must be a number

Để xử lý ngoại lệ, bạn có thể sử dụng câu lệnh

Uncaught The first argument must be a number

6. Ví dụ

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } try { const result = add('a', 10); console.log(result); } catch (e) { console.log(e); }

Code language: JavaScript (javascript)

đầu ra

The first argument must be a number

Trong ví dụ này, chúng tôi đặt lệnh gọi hàm

Uncaught The first argument must be a number

1 trong khối

Uncaught The first argument must be a number

8. Bởi vì

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
4 trong câu lệnh

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
1 là một chuỗi, nên ngoại lệ trong khối

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
8 là một chuỗi như được hiển thị trong kết quả

2) Sử dụng câu lệnh ném JavaScript để ném một thể hiện của lớp Lỗi

Trong ví dụ sau, chúng ta đưa ra một thể hiện của lớp

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
5 thay vì một chuỗi trong hàm

Uncaught The first argument must be a number

1;

function add(x, y) { if (typeof x !== 'number') { throw new Error('The first argument must be a number'); } if (typeof y !== 'number') { throw new Error('The second argument must be a number'); } return x + y; } try { const result = add('a', 10); console.log(result); } catch (e) { console.log(e.name, ':', e.message); }

Code language: JavaScript (javascript)

đầu ra

Error : The first argument must be a number

Code language: JavaScript (javascript)

Như được hiển thị trong đầu ra, đối tượng ngoại lệ trong khối

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
8 có

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } try { const result = add('a', 10); console.log(result); } catch (e) { console.log(e); }

Code language: JavaScript (javascript)
5 là

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
5 và

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } try { const result = add('a', 10); console.log(result); } catch (e) { console.log(e); }

Code language: JavaScript (javascript)
7 là đối tượng mà chúng ta chuyển đến hàm tạo

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } try { const result = add('a', 10); console.log(result); } catch (e) { console.log(e); }

Code language: JavaScript (javascript)
8

3) Sử dụng câu lệnh ném JavaScript để ném ngoại lệ do người dùng xác định

Đôi khi, bạn muốn đưa ra một lỗi tùy chỉnh thay vì

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
5 tích hợp sẵn. Để làm điều đó, bạn có thể định nghĩa một lớp lỗi tùy chỉnh mở rộng lớp

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
5 và ném một phiên bản mới của lớp đó. Ví dụ

Đầu tiên, định nghĩa

The first argument must be a number

1 mở rộng lớp

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
5

class NumberError extends Error { constructor(value) { super(`"${value}" is not a valid number`); this.name = 'InvalidNumber'; } }

Code language: JavaScript (javascript)

The first argument must be a number

3 của lớp

The first argument must be a number

1 chấp nhận một giá trị mà bạn sẽ chuyển vào nó khi tạo một thể hiện mới của lớp

Trong lớp

The first argument must be a number

3 của lớp

The first argument must be a number

6, chúng ta gọi hàm tạo của lớp

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
5 thông qua lớp

The first argument must be a number

8 và truyền một chuỗi cho nó. Ngoài ra, chúng tôi ghi đè tên của lỗi thành chuỗi ký tự

The first argument must be a number

1. Nếu chúng tôi không làm điều này, thì

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } try { const result = add('a', 10); console.log(result); } catch (e) { console.log(e); }

Code language: JavaScript (javascript)
5 của

The first argument must be a number

1 sẽ là

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
5

Thứ hai, sử dụng lớp

The first argument must be a number

1 trong hàm

Uncaught The first argument must be a number

1

function add(x, y) { if (typeof x !== 'number') { throw new NumberError(x); } if (typeof y !== 'number') { throw new NumberError(y); } return x + y; }

Code language: JavaScript (javascript)

Trong hàm

Uncaught The first argument must be a number

1, chúng ta đưa ra một thể hiện của lớp

The first argument must be a number

1 nếu đối số không phải là số hợp lệ

Thứ ba, bắt ngoại lệ do hàm

Uncaught The first argument must be a number

1 đưa ra

try { const result = add('a', 10); console.log(result); } catch (e) { console.log(e.name, ':', e.message); }

Code language: JavaScript (javascript)

đầu ra

function add(x, y) { if (typeof x !== 'number') { throw 'The first argument must be a number'; } if (typeof y !== 'number') { throw 'The second argument must be a number'; } return x + y; } const result = add('a', 10); console.log(result);

Code language: JavaScript (javascript)
0

Trong ví dụ này, tên ngoại lệ là

The first argument must be a number

1 và thông báo là thông báo mà chúng tôi chuyển vào

function add(x, y) { if (typeof x !== 'number') { throw new Error('The first argument must be a number'); } if (typeof y !== 'number') { throw new Error('The second argument must be a number'); } return x + y; } try { const result = add('a', 10); console.log(result); } catch (e) { console.log(e.name, ':', e.message); }

Code language: JavaScript (javascript)
9 trong

The first argument must be a number

3 của lớp

The first argument must be a number

1

Làm cách nào để tạo một lỗi mới trong JavaScript?

function CustomException(tin nhắn) { const error = new Error(tin nhắn); . nguyên mẫu = Đối tượng. tạo (Lỗi. nguyên mẫu);

Làm cách nào để ném lỗi JavaScript?

Vì chúng tôi biết hai loại lỗi JavaScript chính nên chúng tôi có thể nhanh chóng chuyển sang sửa chúng. .
Khi nghi ngờ, hãy tải lại. Đôi khi điều đơn giản nhất chúng ta có thể làm là tải lại trang web. .
Kiểm tra xem JavaScript đã được bật chưa. .
Sử dụng Công cụ dành cho nhà phát triển trong trình duyệt. .
Kiểm tra khả năng tương thích giữa các trình duyệt. .
Sử dụng bảng điều khiển. .
Đến cuối cùng

Làm cách nào để đưa ra lỗi tùy chỉnh trong JavaScript?

Bạn có thể sử dụng lớp lỗi này để xây dựng nguyên mẫu đối tượng lỗi của riêng mình, được gọi là lỗi tùy chỉnh. Lỗi tùy chỉnh có thể được xây dựng theo hai cách, đó là. Trình tạo lớp mở rộng lớp lỗi. .
tin nhắn. Thông báo lỗi
tên tập tin. Tệp xảy ra lỗi
vải lanh. số dòng lỗi

Làm cách nào để đưa ra lỗi 500 trong JavaScript?

Lỗi thường được lan truyền thông qua tham số đầu tiên của lệnh gọi lại được cung cấp. Nếu tôi cần trả lại lỗi cho người dùng, tôi sẽ thực hiện bằng cách sử dụng res. gửi('Lỗi', 500);