0
0
Javascriptprogramming~20 mins

Throwing errors in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Error Throwing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output when throwing a string error?

Consider this JavaScript code that throws a string as an error. What will be printed to the console?

Javascript
try {
  throw "Oops! Something went wrong.";
} catch (e) {
  console.log(e);
}
AError: Oops! Something went wrong.
BOops! Something went wrong.
Cundefined
DSyntaxError
Attempts:
2 left
πŸ’‘ Hint

When you throw a string, the catch block receives that string directly.

❓ Predict Output
intermediate
2:00remaining
What error is thrown by this code?

What error will be thrown by this code snippet?

Javascript
function test() {
  throw new TypeError('Wrong type!');
}
try {
  test();
} catch (e) {
  console.log(e.name);
}
AError
BSyntaxError
CReferenceError
DTypeError
Attempts:
2 left
πŸ’‘ Hint

Look at the type of error created with new.

❓ Predict Output
advanced
2:00remaining
What happens if you throw an object without message property?

What will be the output of this code?

Javascript
try {
  throw {code: 404};
} catch (e) {
  console.log(e.message);
}
Aundefined
B404
CError: 404
DTypeError
Attempts:
2 left
πŸ’‘ Hint

The thrown object does not have a message property.

❓ Predict Output
advanced
2:00remaining
What is the output when throwing a custom error class?

What will this code print?

Javascript
class CustomError extends Error {
  constructor(msg) {
    super(msg);
    this.name = 'CustomError';
  }
}

try {
  throw new CustomError('Custom problem');
} catch (e) {
  console.log(e.name + ': ' + e.message);
}
ACustomError: Custom problem
BTypeError: Custom problem
CCustomError
DError: Custom problem
Attempts:
2 left
πŸ’‘ Hint

The custom error sets its name property explicitly.

❓ Predict Output
expert
2:00remaining
What is the output when throwing inside a Promise executor?

What will this code print to the console?

Javascript
new Promise((resolve, reject) => {
  throw new Error('Promise failed');
}).catch(e => {
  console.log(e.message);
});
AUncaught Error
Bundefined
CPromise failed
DSyntaxError
Attempts:
2 left
πŸ’‘ Hint

Throwing inside a Promise executor triggers the rejection.