Trình xây dựng Error[]
tạo ra một đối tượng lỗi.Error[]
constructor creates an error object.Cú pháp
new Error[]
new Error[message]
new Error[message, options]
new Error[message, fileName]
new Error[message, fileName, lineNumber]
Error[]
Error[message]
Error[message, options]
Error[message, fileName]
Error[message, fileName, lineNumber]
Lưu ý: Error[]
có thể được gọi có hoặc không có
0. Cả hai đều tạo ra một thể hiện const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 mới. const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
Error[]
can be called with or without
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
0. Both create a new const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 instance.Thông số
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
2 Tùy chọnOptionalMột mô tả có thể đọc được của con người về lỗi.
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
3 Tùy chọnOptionalMột đối tượng có các thuộc tính sau:
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
4 Tùy chọnOptionalMột thuộc tính chỉ ra nguyên nhân cụ thể của lỗi. Khi bắt và ném lại lỗi với thông báo lỗi cụ thể hoặc hữu ích hơn, thuộc tính này có thể được sử dụng để truyền lỗi ban đầu.
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
5 tùy chọn-tiêu chuẩnOptional
Non-standardGiá trị cho thuộc tính
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
5 trên đối tượng const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 được tạo. Mặc định với tên của tệp chứa mã được gọi là hàm tạo Error[]
.const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
9 tùy chọn-tiêu chuẩnOptional Non-standardGiá trị cho thuộc tính
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
9 trên đối tượng const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 được tạo. Mặc định với số dòng chứa lệnh gọi hàm tạo Error[]
.Ví dụ
Gọi chức năng hoặc xây dựng mới
Khi
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 được sử dụng như một hàm, đó là không có ____10, nó sẽ trả về một đối tượng const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1. Do đó, một cuộc gọi đơn thuần đến const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 sẽ tạo ra cùng một đầu ra xây dựng đối tượng const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 thông qua từ khóa const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
0 sẽ.const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
Phát triển lại một lỗi với một nguyên nhân
Đôi khi rất hữu ích để bắt một lỗi và ném lại nó bằng một thông báo mới. Trong trường hợp này, bạn nên chuyển lỗi ban đầu vào hàm tạo cho
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 mới, như được hiển thị.try {
frameworkThatCanThrow[];
} catch [err] {
throw new Error["New error message", { cause: err }];
}
Để biết ví dụ chi tiết hơn, hãy xem Lỗi> Phân biệt giữa các lỗi tương tự.
Thông số kỹ thuật
Đặc tả ngôn ngữ Ecmascript # Sec-Set-Constructor # sec-error-constructor |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt
Xem thêm
- Một polyfill của
1 với hành vi hiện đại như hỗ trợconst x = Error["I was created using a function call!"]; // above has the same functionality as following const y = new Error['I was constructed via the "new" keyword!'];
4 có sẵn trongconst x = Error["I was created using a function call!"]; // above has the same functionality as following const y = new Error['I was constructed via the "new" keyword!'];
2try { throw new Error["Whoops!"]; } catch [e] { console.error[`${e.name}: ${e.message}`]; }
3try { throw new Error["Whoops!"]; } catch [e] { console.error[`${e.name}: ${e.message}`]; }
4try { throw new Error["Whoops!"]; } catch [e] { console.error[`${e.name}: ${e.message}`]; }
- Nguyên nhân lỗi [v8.dev/eatures]
Các đối tượng
1 được ném khi xảy ra lỗi thời gian chạy. Đối tượng const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 cũng có thể được sử dụng làm đối tượng cơ sở cho các ngoại lệ do người dùng xác định. Xem bên dưới để biết các loại lỗi tích hợp tiêu chuẩn.const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
Sự mô tả
Lỗi thời gian chạy dẫn đến các đối tượng
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 mới được tạo và ném.const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 là một đối tượng có thể nối tiếp, vì vậy nó có thể được nhân bản với try {
throw new Error["Whoops!"];
} catch [e] {
console.error[`${e.name}: ${e.message}`];
}
9 hoặc được sao chép giữa các công nhân bằng cách sử dụng try {
foo.bar[];
} catch [e] {
if [e instanceof EvalError] {
console.error[`${e.name}: ${e.message}`];
} else if [e instanceof RangeError] {
console.error[`${e.name}: ${e.message}`];
}
// etc.
else {
// If none of our cases matched leave the Error unhandled
throw e;
}
}
0.Loại lỗi
Bên cạnh hàm tạo
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 chung, còn có các hàm tạo lỗi cốt lõi khác trong JavaScript. Đối với các ngoại lệ phía máy khách, hãy xem các báo cáo xử lý ngoại lệ.try {
foo.bar[];
} catch [e] {
if [e instanceof EvalError] {
console.error[`${e.name}: ${e.message}`];
} else if [e instanceof RangeError] {
console.error[`${e.name}: ${e.message}`];
}
// etc.
else {
// If none of our cases matched leave the Error unhandled
throw e;
}
}
2Tạo một thể hiện biểu thị một lỗi xảy ra liên quan đến hàm toàn cầu
try {
foo.bar[];
} catch [e] {
if [e instanceof EvalError] {
console.error[`${e.name}: ${e.message}`];
} else if [e instanceof RangeError] {
console.error[`${e.name}: ${e.message}`];
}
// etc.
else {
// If none of our cases matched leave the Error unhandled
throw e;
}
}
3.try {
foo.bar[];
} catch [e] {
if [e instanceof EvalError] {
console.error[`${e.name}: ${e.message}`];
} else if [e instanceof RangeError] {
console.error[`${e.name}: ${e.message}`];
}
// etc.
else {
// If none of our cases matched leave the Error unhandled
throw e;
}
}
4Tạo một thể hiện biểu thị một lỗi xảy ra khi một biến hoặc tham số số nằm ngoài phạm vi hợp lệ của nó.
try {
foo.bar[];
} catch [e] {
if [e instanceof EvalError] {
console.error[`${e.name}: ${e.message}`];
} else if [e instanceof RangeError] {
console.error[`${e.name}: ${e.message}`];
}
// etc.
else {
// If none of our cases matched leave the Error unhandled
throw e;
}
}
5Tạo một thể hiện biểu thị một lỗi xảy ra khi không tham chiếu tham chiếu không hợp lệ.
try {
foo.bar[];
} catch [e] {
if [e instanceof EvalError] {
console.error[`${e.name}: ${e.message}`];
} else if [e instanceof RangeError] {
console.error[`${e.name}: ${e.message}`];
}
// etc.
else {
// If none of our cases matched leave the Error unhandled
throw e;
}
}
6Tạo một thể hiện đại diện cho một lỗi cú pháp.
try {
foo.bar[];
} catch [e] {
if [e instanceof EvalError] {
console.error[`${e.name}: ${e.message}`];
} else if [e instanceof RangeError] {
console.error[`${e.name}: ${e.message}`];
}
// etc.
else {
// If none of our cases matched leave the Error unhandled
throw e;
}
}
7Tạo một thể hiện biểu thị một lỗi xảy ra khi một biến hoặc tham số không thuộc loại hợp lệ.
try {
foo.bar[];
} catch [e] {
if [e instanceof EvalError] {
console.error[`${e.name}: ${e.message}`];
} else if [e instanceof RangeError] {
console.error[`${e.name}: ${e.message}`];
}
// etc.
else {
// If none of our cases matched leave the Error unhandled
throw e;
}
}
8Tạo một thể hiện biểu thị một lỗi xảy ra khi
try {
foo.bar[];
} catch [e] {
if [e instanceof EvalError] {
console.error[`${e.name}: ${e.message}`];
} else if [e instanceof RangeError] {
console.error[`${e.name}: ${e.message}`];
}
// etc.
else {
// If none of our cases matched leave the Error unhandled
throw e;
}
}
9 hoặc function doWork[] {
try {
doFailSomeWay[];
} catch [err] {
throw new Error["Failed in some way", { cause: err }];
}
try {
doFailAnotherWay[];
} catch [err] {
throw new Error["Failed in another way", { cause: err }];
}
}
try {
doWork[];
} catch [err] {
switch [err.message] {
case "Failed in some way":
handleFailSomeWay[err.cause];
break;
case "Failed in another way":
handleFailAnotherWay[err.cause];
break;
}
}
0 được truyền các tham số không hợp lệ.function doWork[] {
try {
doFailSomeWay[];
} catch [err] {
throw new Error["Failed in some way", { cause: err }];
}
try {
doFailAnotherWay[];
} catch [err] {
throw new Error["Failed in another way", { cause: err }];
}
}
try {
doWork[];
} catch [err] {
switch [err.message] {
case "Failed in some way":
handleFailSomeWay[err.cause];
break;
case "Failed in another way":
handleFailAnotherWay[err.cause];
break;
}
}
1Tạo một thể hiện đại diện cho một số lỗi được bọc trong một lỗi duy nhất khi cần có nhiều lỗi được báo cáo bởi một thao tác, ví dụ như
function doWork[] {
try {
doFailSomeWay[];
} catch [err] {
throw new Error["Failed in some way", { cause: err }];
}
try {
doFailAnotherWay[];
} catch [err] {
throw new Error["Failed in another way", { cause: err }];
}
}
try {
doWork[];
} catch [err] {
switch [err.message] {
case "Failed in some way":
handleFailSomeWay[err.cause];
break;
case "Failed in another way":
handleFailAnotherWay[err.cause];
break;
}
}
2.function doWork[] {
try {
doFailSomeWay[];
} catch [err] {
throw new Error["Failed in some way", { cause: err }];
}
try {
doFailAnotherWay[];
} catch [err] {
throw new Error["Failed in another way", { cause: err }];
}
}
try {
doWork[];
} catch [err] {
switch [err.message] {
case "Failed in some way":
handleFailSomeWay[err.cause];
break;
case "Failed in another way":
handleFailAnotherWay[err.cause];
break;
}
}
3 không chuẩnNon-standardTạo một thể hiện biểu thị một lỗi xảy ra khi lỗi bên trong trong công cụ JavaScript bị ném. Ví dụ. "Quá nhiều đệ quy".
Người xây dựng
Error[]
Tạo một đối tượng
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 mới.Phương pháp tĩnh
function doWork[] {
try {
doFailSomeWay[];
} catch [err] {
throw new Error["Failed in some way", { cause: err }];
}
try {
doFailAnotherWay[];
} catch [err] {
throw new Error["Failed in another way", { cause: err }];
}
}
try {
doWork[];
} catch [err] {
switch [err.message] {
case "Failed in some way":
handleFailSomeWay[err.cause];
break;
case "Failed in another way":
handleFailAnotherWay[err.cause];
break;
}
}
6 không chuẩnNon-standardHàm V8 không chuẩn tạo thuộc tính
function doWork[] {
try {
doFailSomeWay[];
} catch [err] {
throw new Error["Failed in some way", { cause: err }];
}
try {
doFailAnotherWay[];
} catch [err] {
throw new Error["Failed in another way", { cause: err }];
}
}
try {
doWork[];
} catch [err] {
switch [err.message] {
case "Failed in some way":
handleFailSomeWay[err.cause];
break;
case "Failed in another way":
handleFailAnotherWay[err.cause];
break;
}
}
7 trên một thể hiện lỗi.function doWork[] {
try {
doFailSomeWay[];
} catch [err] {
throw new Error["Failed in some way", { cause: err }];
}
try {
doFailAnotherWay[];
} catch [err] {
throw new Error["Failed in another way", { cause: err }];
}
}
try {
doWork[];
} catch [err] {
switch [err.message] {
case "Failed in some way":
handleFailSomeWay[err.cause];
break;
case "Failed in another way":
handleFailAnotherWay[err.cause];
break;
}
}
8 không chuẩnNon-standardMột thuộc tính số V8 không chuẩn, giới hạn số lượng khung ngăn xếp trong một stacktrace lỗi.
function doWork[] {
try {
doFailSomeWay[];
} catch [err] {
throw new Error["Failed in some way", { cause: err }];
}
try {
doFailAnotherWay[];
} catch [err] {
throw new Error["Failed in another way", { cause: err }];
}
}
try {
doWork[];
} catch [err] {
switch [err.message] {
case "Failed in some way":
handleFailSomeWay[err.cause];
break;
case "Failed in another way":
handleFailAnotherWay[err.cause];
break;
}
}
9 không chuẩnNon-standard OptionalChức năng V8 không chuẩn, nếu được cung cấp bởi UserCode, được gọi bởi động cơ JavaScript V8 cho các ngoại lệ ném, cho phép người dùng cung cấp định dạng tùy chỉnh cho các ngăn xếp.
Thuộc tính thể hiện
class MyError extends Error {
constructor[message, options] {
// Need to pass `options` as the second parameter to install the "cause" property.
super[message, options];
}
}
console.log[new MyError["test", { cause: new Error["cause"] }].cause];
// Error: cause
0Thông báo lỗi. Đối với các đối tượng
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 do người dùng tạo, đây là chuỗi được cung cấp dưới dạng đối số đầu tiên của trình xây dựng.class MyError extends Error {
constructor[message, options] {
// Need to pass `options` as the second parameter to install the "cause" property.
super[message, options];
}
}
console.log[new MyError["test", { cause: new Error["cause"] }].cause];
// Error: cause
2Tên lỗi. Điều này được xác định bởi hàm tạo hàm.
class MyError extends Error {
constructor[message, options] {
// Need to pass `options` as the second parameter to install the "cause" property.
super[message, options];
}
}
console.log[new MyError["test", { cause: new Error["cause"] }].cause];
// Error: cause
3Lỗi nguyên nhân chỉ ra lý do tại sao lỗi hiện tại được ném - thường là một lỗi bị bắt khác. Đối với các đối tượng
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 do người dùng tạo, đây là giá trị được cung cấp dưới dạng thuộc tính const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
4 của đối số thứ hai của nhà xây dựng.class MyError extends Error {
constructor[message, options] {
// Need to pass `options` as the second parameter to install the "cause" property.
super[message, options];
}
}
console.log[new MyError["test", { cause: new Error["cause"] }].cause];
// Error: cause
6 không chuẩnNon-standardMột thuộc tính Mozilla không chuẩn cho đường dẫn đến tệp đã nêu ra lỗi này.
class MyError extends Error {
constructor[message, options] {
// Need to pass `options` as the second parameter to install the "cause" property.
super[message, options];
}
}
console.log[new MyError["test", { cause: new Error["cause"] }].cause];
// Error: cause
7 không chuẩnNon-standardMột thuộc tính Mozilla không chuẩn cho số dòng trong tệp đã nêu lỗi này.
class MyError extends Error {
constructor[message, options] {
// Need to pass `options` as the second parameter to install the "cause" property.
super[message, options];
}
}
console.log[new MyError["test", { cause: new Error["cause"] }].cause];
// Error: cause
8 không chuẩnNon-standardMột thuộc tính Mozilla không chuẩn cho số cột trong dòng đã nêu ra lỗi này.
class MyError extends Error {
constructor[message, options] {
// Need to pass `options` as the second parameter to install the "cause" property.
super[message, options];
}
}
console.log[new MyError["test", { cause: new Error["cause"] }].cause];
// Error: cause
9 không chuẩnNon-standardMột tài sản không chuẩn cho một dấu vết ngăn xếp.
Phương pháp thể hiện
class CustomError extends Error {
constructor[foo = "bar", ...params] {
// Pass remaining arguments [including vendor specific ones] to parent constructor
super[...params];
// Maintains proper stack trace for where our error was thrown [only available on V8]
if [Error.captureStackTrace] {
Error.captureStackTrace[this, CustomError];
}
this.name = "CustomError";
// Custom debugging information
this.foo = foo;
this.date = new Date[];
}
}
try {
throw new CustomError["baz", "bazMessage"];
} catch [e] {
console.error[e.name]; // CustomError
console.error[e.foo]; // baz
console.error[e.message]; // bazMessage
console.error[e.stack]; // stacktrace
}
0Trả về một chuỗi đại diện cho đối tượng được chỉ định. Ghi đè phương thức
class CustomError extends Error {
constructor[foo = "bar", ...params] {
// Pass remaining arguments [including vendor specific ones] to parent constructor
super[...params];
// Maintains proper stack trace for where our error was thrown [only available on V8]
if [Error.captureStackTrace] {
Error.captureStackTrace[this, CustomError];
}
this.name = "CustomError";
// Custom debugging information
this.foo = foo;
this.date = new Date[];
}
}
try {
throw new CustomError["baz", "bazMessage"];
} catch [e] {
console.error[e.name]; // CustomError
console.error[e.foo]; // baz
console.error[e.message]; // bazMessage
console.error[e.stack]; // stacktrace
}
1.Ví dụ
Ném một lỗi chung
Thông thường bạn tạo một đối tượng
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 với mục đích nâng cao nó bằng cách sử dụng từ khóa try {
throw new Error["Whoops!"];
} catch [e] {
console.error[`${e.name}: ${e.message}`];
}
3. Bạn có thể xử lý lỗi bằng cách sử dụng cấu trúc try {
throw new Error["Whoops!"];
} catch [e] {
console.error[`${e.name}: ${e.message}`];
}
4:try {
throw new Error["Whoops!"];
} catch [e] {
console.error[`${e.name}: ${e.message}`];
}
Xử lý một loại lỗi cụ thể
Bạn có thể chọn chỉ xử lý các loại lỗi cụ thể bằng cách kiểm tra loại lỗi với thuộc tính
class CustomError extends Error {
constructor[foo = "bar", ...params] {
// Pass remaining arguments [including vendor specific ones] to parent constructor
super[...params];
// Maintains proper stack trace for where our error was thrown [only available on V8]
if [Error.captureStackTrace] {
Error.captureStackTrace[this, CustomError];
}
this.name = "CustomError";
// Custom debugging information
this.foo = foo;
this.date = new Date[];
}
}
try {
throw new CustomError["baz", "bazMessage"];
} catch [e] {
console.error[e.name]; // CustomError
console.error[e.foo]; // baz
console.error[e.message]; // bazMessage
console.error[e.stack]; // stacktrace
}
5 của lỗi hoặc, nếu bạn đang viết cho các công cụ JavaScript hiện đại, từ khóa class CustomError extends Error {
constructor[foo = "bar", ...params] {
// Pass remaining arguments [including vendor specific ones] to parent constructor
super[...params];
// Maintains proper stack trace for where our error was thrown [only available on V8]
if [Error.captureStackTrace] {
Error.captureStackTrace[this, CustomError];
}
this.name = "CustomError";
// Custom debugging information
this.foo = foo;
this.date = new Date[];
}
}
try {
throw new CustomError["baz", "bazMessage"];
} catch [e] {
console.error[e.name]; // CustomError
console.error[e.foo]; // baz
console.error[e.message]; // bazMessage
console.error[e.stack]; // stacktrace
}
6: từ khóa:try {
foo.bar[];
} catch [e] {
if [e instanceof EvalError] {
console.error[`${e.name}: ${e.message}`];
} else if [e instanceof RangeError] {
console.error[`${e.name}: ${e.message}`];
}
// etc.
else {
// If none of our cases matched leave the Error unhandled
throw e;
}
}
Phân biệt giữa các lỗi tương tự
Đôi khi, một khối mã có thể thất bại vì những lý do yêu cầu xử lý khác nhau, nhưng ném các lỗi rất giống nhau [nghĩa là với cùng loại và thông báo].
Nếu bạn không có quyền kiểm soát các lỗi ban đầu được ném, một tùy chọn là bắt chúng và ném các đối tượng
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 mới có thông điệp cụ thể hơn. Lỗi ban đầu phải được chuyển cho const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 mới trong tham số const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
3 của nhà xây dựng dưới dạng thuộc tính const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
4 của nó. Điều này đảm bảo rằng lỗi gốc và dấu vết ngăn xếp có sẵn cho các khối thử/bắt cấp cao hơn.Ví dụ dưới đây cho thấy điều này cho hai phương pháp sẽ thất bại với các lỗi tương tự [Error[]
1 và Error[]
2]:
function doWork[] {
try {
doFailSomeWay[];
} catch [err] {
throw new Error["Failed in some way", { cause: err }];
}
try {
doFailAnotherWay[];
} catch [err] {
throw new Error["Failed in another way", { cause: err }];
}
}
try {
doWork[];
} catch [err] {
switch [err.message] {
case "Failed in some way":
handleFailSomeWay[err.cause];
break;
case "Failed in another way":
handleFailAnotherWay[err.cause];
break;
}
}
Lưu ý: Nếu bạn đang tạo thư viện, bạn nên sử dụng lỗi gây ra lỗi để phân biệt giữa các lỗi khác nhau - thay vì yêu cầu người tiêu dùng của bạn phân tích thông báo lỗi. Xem trang Nguyên nhân lỗi cho một ví dụ. If you are making a library, you should prefer to use error cause to discriminate between different errors emitted — rather than asking your consumers to parse the error message. See the error cause page for an example.
Các loại lỗi tùy chỉnh cũng có thể sử dụng thuộc tính
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
4, với điều kiện hàm tạo của các lớp con vượt qua tham số const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
3 khi gọi Error[]
5. Trình xây dựng lớp cơ sở Error[]
sẽ đọc Error[]
7 và xác định thuộc tính const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
4 trên thể hiện lỗi mới.class MyError extends Error {
constructor[message, options] {
// Need to pass `options` as the second parameter to install the "cause" property.
super[message, options];
}
}
console.log[new MyError["test", { cause: new Error["cause"] }].cause];
// Error: cause
Các loại lỗi tùy chỉnh
Bạn có thể muốn xác định các loại lỗi của riêng mình xuất phát từ
const x = Error["I was created using a function call!"];
// above has the same functionality as following
const y = new Error['I was constructed via the "new" keyword!'];
1 để có thể Error[]
0 và sử dụng Error[]
1 để kiểm tra loại lỗi trong trình xử lý ngoại lệ. Điều này dẫn đến mã xử lý lỗi sạch hơn và nhất quán hơn.Xem "Cách tốt để gia hạn lỗi trong JavaScript là gì?" trên stackoverflow cho một cuộc thảo luận chuyên sâu.
Lưu ý: Một số trình duyệt bao gồm hàm tạo Error[]
2 trong dấu vết ngăn xếp khi sử dụng các lớp ES2015. Some browsers include the Error[]
2 constructor in the stack trace when using ES2015 classes.
class CustomError extends Error {
constructor[foo = "bar", ...params] {
// Pass remaining arguments [including vendor specific ones] to parent constructor
super[...params];
// Maintains proper stack trace for where our error was thrown [only available on V8]
if [Error.captureStackTrace] {
Error.captureStackTrace[this, CustomError];
}
this.name = "CustomError";
// Custom debugging information
this.foo = foo;
this.date = new Date[];
}
}
try {
throw new CustomError["baz", "bazMessage"];
} catch [e] {
console.error[e.name]; // CustomError
console.error[e.foo]; // baz
console.error[e.message]; // bazMessage
console.error[e.stack]; // stacktrace
}
Thông số kỹ thuật
Đặc tả ngôn ngữ Ecmascript # Sec-Error-Epjects # sec-error-objects |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt
Xem thêm
- Một polyfill của
1 với hành vi hiện đại như hỗ trợconst x = Error["I was created using a function call!"]; // above has the same functionality as following const y = new Error['I was constructed via the "new" keyword!'];
4 có sẵn trongconst x = Error["I was created using a function call!"]; // above has the same functionality as following const y = new Error['I was constructed via the "new" keyword!'];
2try { throw new Error["Whoops!"]; } catch [e] { console.error[`${e.name}: ${e.message}`]; }
3try { throw new Error["Whoops!"]; } catch [e] { console.error[`${e.name}: ${e.message}`]; }
4try { throw new Error["Whoops!"]; } catch [e] { console.error[`${e.name}: ${e.message}`]; }
- Tài liệu V8 cho
6,function doWork[] { try { doFailSomeWay[]; } catch [err] { throw new Error["Failed in some way", { cause: err }]; } try { doFailAnotherWay[]; } catch [err] { throw new Error["Failed in another way", { cause: err }]; } } try { doWork[]; } catch [err] { switch [err.message] { case "Failed in some way": handleFailSomeWay[err.cause]; break; case "Failed in another way": handleFailAnotherWay[err.cause]; break; } }
8 vàfunction doWork[] { try { doFailSomeWay[]; } catch [err] { throw new Error["Failed in some way", { cause: err }]; } try { doFailAnotherWay[]; } catch [err] { throw new Error["Failed in another way", { cause: err }]; } } try { doWork[]; } catch [err] { switch [err.message] { case "Failed in some way": handleFailSomeWay[err.cause]; break; case "Failed in another way": handleFailAnotherWay[err.cause]; break; } }
9.function doWork[] { try { doFailSomeWay[]; } catch [err] { throw new Error["Failed in some way", { cause: err }]; } try { doFailAnotherWay[]; } catch [err] { throw new Error["Failed in another way", { cause: err }]; } } try { doWork[]; } catch [err] { switch [err.message] { case "Failed in some way": handleFailSomeWay[err.cause]; break; case "Failed in another way": handleFailAnotherWay[err.cause]; break; } }