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
1 để đưa ra một ngoại lệCode language: JavaScript [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];
Giới thiệu về câu lệnh ném JavaScript
Câu lệnh
1 cho phép bạn đưa ra một ngoại lệ. Đây là cú pháp của câu lệnhCode language: JavaScript [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];
1Code language: JavaScript [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]
throw expression;
Trong cú pháp này,
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ớpCode language: JavaScript [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];
5 hoặc các lớp con của nóCode language: JavaScript [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];
Khi gặp câu lệnh
1, công cụ JavaScript ngừng thực thi và chuyển điều khiển tới khốiCode language: JavaScript [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];
7 đầu tiên trong ngăn xếp cuộc gọi. Nếu không có khốiCode language: JavaScript [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];
8 nào tồn tại, công cụ JavaScript sẽ chấm dứt tập lệnhCode language: JavaScript [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];
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
1Code language: JavaScript [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];
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
1 để đưa ra một ngoại lệ trong một hàmCode language: JavaScript [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]
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];
Làm thế nào nó hoạt động
Trước tiên, hãy xác định hàm
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ố
Uncaught The first argument must be a number
Thứ hai, gọi hàm
1 và truyền một chuỗi và một số vào đó
Uncaught The first argument must be a number
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
6. Ví dụ
Uncaught The first argument must be a number
Code language: JavaScript [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; } try { const result = add['a', 10]; console.log[result]; } catch [e] { console.log[e]; }
đầ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
1 trong khối
Uncaught The first argument must be a number
8. Bởi vì
Uncaught The first argument must be a number
4 trong câu lệnhCode language: JavaScript [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];
1 là một chuỗi, nên ngoại lệ trong khốiCode language: JavaScript [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];
8 là một chuỗi như được hiển thị trong kết quảCode language: JavaScript [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];
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
5 thay vì một chuỗi trong hàmCode language: JavaScript [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];
1;
Uncaught The first argument must be a number
Code language: JavaScript [javascript]
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]; }
đầu ra
Code language: JavaScript [javascript]
Error : The first argument must be a number
Như được hiển thị trong đầu ra, đối tượng ngoại lệ trong khối
8 cóCode language: JavaScript [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];
5 làCode language: JavaScript [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; } try { const result = add['a', 10]; console.log[result]; } catch [e] { console.log[e]; }
5 vàCode language: JavaScript [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];
7 là đối tượng mà chúng ta chuyển đến hàm tạoCode language: JavaScript [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; } try { const result = add['a', 10]; console.log[result]; } catch [e] { console.log[e]; }
8Code language: JavaScript [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; } try { const result = add['a', 10]; console.log[result]; } catch [e] { console.log[e]; }
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ì
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ớpCode language: JavaScript [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];
5 và ném một phiên bản mới của lớp đó. Ví dụCode language: JavaScript [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];
Đầu tiên, định nghĩa
1 mở rộng lớp
The first argument must be a number
5Code language: JavaScript [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]
class NumberError extends Error { constructor[value] { super[`"${value}" is not a valid number`]; this.name = 'InvalidNumber'; } }
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
The first argument must be a number
Trong lớp
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
The first argument must be a number
5 thông qua lớpCode language: JavaScript [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];
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ì
The first argument must be a number
5 củaCode language: JavaScript [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; } try { const result = add['a', 10]; console.log[result]; } catch [e] { console.log[e]; }
1 sẽ là
The first argument must be a number
5Code language: JavaScript [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];
Thứ hai, sử dụng lớp
1 trong hàm
The first argument must be a number
1
Uncaught The first argument must be a number
Code language: JavaScript [javascript]
function add[x, y] { if [typeof x !== 'number'] { throw new NumberError[x]; } if [typeof y !== 'number'] { throw new NumberError[y]; } return x + y; }
Trong hàm
1, chúng ta đưa ra một thể hiện của lớp
Uncaught The first argument must be a number
1 nếu đối số không phải là số hợp lệ
The first argument must be a number
Thứ ba, bắt ngoại lệ do hàm
1 đưa ra
Uncaught The first argument must be a number
Code language: JavaScript [javascript]
try { const result = add['a', 10]; console.log[result]; } catch [e] { console.log[e.name, ':', e.message]; }
đầu ra
0Code language: JavaScript [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];
Trong ví dụ này, tên ngoại lệ là
1 và thông báo là thông báo mà chúng tôi chuyển vào
The first argument must be a number
9 trongCode language: JavaScript [javascript]
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]; }
3 của lớp
The first argument must be a number
1
The first argument must be a number