Câu hỏi
Kết quả đoạn code sau là gì?
function greeting() {
throw "Hello world!";
}
function sayHi() {
try {
const data = greeting();
console.log("It worked!", data);
} catch (e) {
console.log("Oh no an error!", e);
}
}
sayHi();
- A:
"It worked! Hello world!"
- B:
"Oh no an error: undefined
- C:
SyntaxError: can only throw Error objects
- D:
"Oh no an error: Hello world!
Đáp án: D
Với lệnh throw
, chúng ta có thể tạo ra các errors tùy ý. Với câu lệnh đó, chúng ta c...