Question 1
try {
console.log("A");
}
catch {
console.log("B");
}
No error occurs, so catch is skipped.
Question 2
try {
unknown();
}
catch {
console.log("Caught");
}
The error is handled by catch.
Question 3
try {
throw new Error(
"Oops"
);
}
catch(error) {
console.log(
error.message
);
}
Error.message contains the supplied message.
Question 4
try {
throw "JS";
}
catch(error) {
console.log(error);
}
JavaScript allows throwing any value.
Question 5
try {
console.log("A");
}
finally {
console.log("B");
}
finally always executes.
Question 6
try {
throw new Error(
"X"
);
}
catch {
console.log("Y");
}
finally {
console.log("Z");
}
catch runs first, then finally.
Question 7
try {
JSON.parse("{");
}
catch(error) {
console.log(
error.name
);
}
Invalid JSON throws a SyntaxError.
Question 8
try {
null.test();
}
catch(error) {
console.log(
error.name
);
}
Calling a method on null causes a TypeError.
Question 9
try {
console.log(x);
}
catch(error) {
console.log(
error.name
);
}
x is not defined.
Question 10
try {
throw new TypeError(
"Wrong"
);
}
catch(error) {
console.log(
error.name
);
}
The thrown error is a TypeError instance.
Question 11
try {
throw new Error(
"JS"
);
}
catch(error) {
console.log(
typeof error
);
}
Error instances are objects.
Question 12
try {
throw new Error(
"React"
);
}
catch(error) {
console.log(
error instanceof Error
);
}
Error objects inherit from Error.
Question 13
try {
console.log("A");
throw new Error();
console.log("B");
}
catch {
console.log("C");
}
Execution stops after throw.
Question 14
try {
throw new Error();
}
catch {
console.log("Catch");
}
finally {
console.log("Finally");
}
finally executes after catch.
Question 15
try {
throw "Hello";
}
catch(error) {
console.log(
typeof error
);
}
The thrown value is a string.
Question 16
class MyError
extends Error {}
try {
throw new MyError(
"Custom"
);
}
catch(error) {
console.log(
error instanceof
MyError
);
}
Custom errors inherit from Error.
Question 17
class MyError
extends Error {}
try {
throw new MyError(
"Oops"
);
}
catch(error) {
console.log(
error.name
);
}
By default, inherited Error classes have name "Error" unless explicitly changed.
Question 18
try {
try {
throw "A";
}
catch {
console.log("B");
}
}
catch {
console.log("C");
}
The inner catch handles the error completely.
Question 19
try {
try {
throw "A";
}
catch {
console.log("B");
throw "C";
}
}
catch(error) {
console.log(error);
}
The inner catch rethrows another error.
Question 20
try {
throw "JS";
}
catch(error) {
throw error;
}
The error is rethrown and remains unhandled.
Question 21
function test() {
try {
return "A";
}
finally {
console.log("B");
}
}
console.log(
test()
);
finally executes before the function actually returns.
Question 22
function test() {
try {
return "A";
}
finally {
return "B";
}
}
console.log(
test()
);
A return inside finally overrides previous returns.
Question 23
function test() {
try {
throw "A";
}
catch {
return "B";
}
finally {
console.log("C");
}
}
console.log(
test()
);
finally executes before the returned value is delivered.
Question 24
try {
throw new RangeError(
"Range"
);
}
catch(error) {
console.log(
error instanceof
RangeError
);
}
RangeError is a built-in error type.
Question 25
try {
decodeURIComponent(
"%"
);
}
catch(error) {
console.log(
error.name
);
}
Invalid URI sequences throw URIError.
Question 26
try {
const arr =
new Array(-1);
}
catch(error) {
console.log(
error.name
);
}
Negative array length causes RangeError.
Question 27
try {
eval(
"if("
);
}
catch(error) {
console.log(
error.name
);
}
Invalid JavaScript syntax throws SyntaxError.
Question 28
try {
throw new Error(
"JS"
);
}
catch(error) {
console.log(
error.message
);
}
message contains the supplied text.
Question 29
try {
throw new Error(
"React"
);
}
catch(error) {
console.log(
error.name +
": " +
error.message
);
}
Both name and message are available.
Question 30
function test() {
try {
throw "A";
}
finally {
return "B";
}
}
console.log(
test()
);
A return in finally suppresses the thrown error.
Question 31
Promise.reject(
"Error"
)
.catch(error => {
console.log(error);
});
catch handles rejected promises.
Question 32
Promise.resolve("JS")
.then(value => {
console.log(value);
});
Resolved values flow into then().
Question 33
Promise.resolve()
.then(() => {
throw "Oops";
})
.catch(error => {
console.log(error);
});
Errors thrown in then() become rejected promises.
Question 34
async function test() {
throw "JS";
}
test().catch(
error => {
console.log(error);
}
);
Thrown values inside async functions become rejected promises.
Question 35
async function test() {
return "React";
}
test().then(
value => {
console.log(value);
}
);
Async functions always return promises.
Question 36
async function test() {
try {
throw "A";
}
catch(error) {
console.log(error);
}
}
test();
The error is handled inside the async function.
Question 37
async function test() {
try {
await Promise.reject(
"Fail"
);
}
catch(error) {
console.log(error);
}
}
test();
await throws the rejection into catch.
Question 38
async function test() {
await Promise.resolve();
return "JS";
}
test().then(
value => {
console.log(value);
}
);
The resolved value is returned after await.
Question 39
class ValidationError
extends Error {}
const error =
new ValidationError(
"Invalid"
);
console.log(
error instanceof
ValidationError
);
Custom errors preserve inheritance.
Question 40
class ValidationError
extends Error {}
const error =
new ValidationError();
console.log(
error instanceof
Error
);
Custom errors are also Error instances.
Question 41
async function test() {
try {
await Promise.reject(
"A"
);
}
catch(error) {
throw "B";
}
}
test().catch(
error => {
console.log(error);
}
);
The catch block throws a new rejection.
Question 42
Promise.reject("A")
.catch(error => {
return "B";
})
.then(value => {
console.log(value);
});
Returning from catch converts the chain back to fulfilled.
Question 43
Promise.reject("A")
.catch(error => {
throw "B";
})
.catch(error => {
console.log(error);
});
Throwing inside catch creates a new rejection.
Question 44
async function test() {
try {
return "A";
}
finally {
return "B";
}
}
test().then(
value => {
console.log(value);
}
);
finally return overrides the try return.
Question 45
async function test() {
try {
throw "A";
}
finally {
return "B";
}
}
test().then(
value => {
console.log(value);
}
);
A return inside finally suppresses both returns and thrown errors.