Challenge - 5 Problems
Runtime Error Catcher
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2: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');
Attempts:
2 left
π‘ Hint
Remember that the catch block runs immediately after the error is thrown.
β Incorrect
The throw statement creates an error that is caught by the catch block, which logs 'Caught: Oops!'. Then the program continues and logs 'Done'.
β Predict Output
intermediate2:00remaining
Output when error is not caught
What happens when this code runs?
Javascript
function test() { JSON.parse('invalid json'); console.log('Parsed'); } test();
Attempts:
2 left
π‘ Hint
Parsing invalid JSON throws an error if not caught.
β Incorrect
JSON.parse throws a SyntaxError on invalid input. Since there is no try-catch, the error stops execution before 'Parsed' logs.
β Predict Output
advanced2: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); }
Attempts:
2 left
π‘ Hint
The inner catch rethrows the error, so the outer catch also runs.
β Incorrect
The inner catch logs the error name, then rethrows it. The outer catch catches it again and logs the message.
β Predict Output
advanced2: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());
Attempts:
2 left
π‘ Hint
The finally block runs after try or catch and can override return values.
β Incorrect
The finally block always runs last and its return overrides any previous return in try or catch.
β Predict Output
expert2: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'); } }
Attempts:
2 left
π‘ Hint
Calling a method on null causes a TypeError.
β Incorrect
null.f() throws a TypeError because null has no properties. The catch checks the error type and logs accordingly.