0
0
Javascriptprogramming~20 mins

Catching runtime errors in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Runtime Error Catcher
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of try-catch with thrown error
What will be the output of this JavaScript code?
Javascript
try {
  throw new Error('Oops!');
} catch (e) {
  console.log('Caught:', e.message);
}
console.log('Done');
ACaught: undefined\nDone
BDone\nCaught: Oops!
CError: Oops!\nDone
DCaught: Oops!\nDone
Attempts:
2 left
πŸ’‘ Hint
Remember that the catch block runs immediately after the error is thrown.
❓ Predict Output
intermediate
2:00remaining
Output when error is not caught
What happens when this code runs?
Javascript
function test() {
  JSON.parse('invalid json');
  console.log('Parsed');
}
test();
Aundefined
BParsed
CSyntaxError is thrown and program stops before 'Parsed' logs
Dnull
Attempts:
2 left
πŸ’‘ Hint
Parsing invalid JSON throws an error if not caught.
❓ Predict Output
advanced
2:00remaining
Output with nested try-catch blocks
What will this code output?
Javascript
try {
  try {
    throw new TypeError('Type issue');
  } catch (e) {
    console.log('Inner catch:', e.name);
    throw e;
  }
} catch (e) {
  console.log('Outer catch:', e.message);
}
AInner catch: TypeError\nOuter catch: Type issue
BOuter catch: Type issue\nInner catch: TypeError
CInner catch: TypeError
DTypeError: Type issue
Attempts:
2 left
πŸ’‘ Hint
The inner catch rethrows the error, so the outer catch also runs.
❓ Predict Output
advanced
2:00remaining
Output when finally block modifies return
What is the output of this code?
Javascript
function check() {
  try {
    return 'try';
  } catch {
    return 'catch';
  } finally {
    return 'finally';
  }
}
console.log(check());
Afinally
Btry
Ccatch
Dundefined
Attempts:
2 left
πŸ’‘ Hint
The finally block runs after try or catch and can override return values.
❓ Predict Output
expert
2:00remaining
Output when catching specific error types
What will this code output?
Javascript
try {
  null.f();
} catch (e) {
  if (e instanceof TypeError) {
    console.log('TypeError caught');
  } else {
    console.log('Other error');
  }
}
AOther error
BTypeError caught
CReferenceError caught
DNo output
Attempts:
2 left
πŸ’‘ Hint
Calling a method on null causes a TypeError.