Bird
Raised Fist0
Javascriptprogramming~20 mins

Catching runtime errors in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of using try and catch in JavaScript?

easy
A. To create new variables
B. To speed up the program execution
C. To handle errors and prevent the program from crashing
D. To repeat code multiple times

Solution

  1. Step 1: Understand the role of try block

    The try block runs code that might cause an error.
  2. Step 2: Understand the role of catch block

    The catch block handles any error from the try block to keep the program running smoothly.
  3. Final Answer:

    To handle errors and prevent the program from crashing -> Option C
  4. Quick Check:

    try-catch = error handling [OK]
Hint: Try-catch blocks catch errors to keep programs running [OK]
Common Mistakes:
  • Thinking try-catch speeds up code
  • Using try-catch to create variables
  • Confusing try-catch with loops
2.

Which of the following is the correct syntax to catch errors in JavaScript?

try {
// code
} catch (error) {
// handle error
}
easy
A. try { } catch (error) { }
B. try { } catch error() { }
C. try (error) { } catch { }
D. try { } catch error { }

Solution

  1. Step 1: Check the try block syntax

    The try block must be followed by catch with parentheses around the error variable.
  2. Step 2: Verify the catch block syntax

    The correct syntax is catch (error) { } with parentheses around the error parameter.
  3. Final Answer:

    try { } catch (error) { } -> Option A
  4. Quick Check:

    catch needs parentheses [OK]
Hint: Catch block always uses parentheses for error [OK]
Common Mistakes:
  • Omitting parentheses in catch
  • Using parentheses after try
  • Adding parentheses after catch keyword
3.

What will be the output of the following code?

try {
let result = 10 / 0;
console.log(result);
} catch (e) {
console.log('Error caught');
}
medium
A. Infinity
B. Error caught
C. 0
D. undefined

Solution

  1. Step 1: Understand division by zero in JavaScript

    Dividing 10 by 0 does not cause an error; it results in Infinity.
  2. Step 2: Check if catch block runs

    Since no error occurs, the catch block is skipped and Infinity is logged.
  3. Final Answer:

    Infinity -> Option A
  4. Quick Check:

    10/0 = Infinity, no error [OK]
Hint: Division by zero returns Infinity, no error thrown [OK]
Common Mistakes:
  • Assuming division by zero throws error
  • Expecting catch block to run
  • Confusing 0 with Infinity
4.

Find the error in this code and choose the correct fix:

try {
console.log(x);
} catch e {
console.log('Error:', e.message);
}
medium
A. Declare x before try block
B. Change catch e to catch (e)
C. Add finally block after catch
D. Remove the catch block

Solution

  1. Step 1: Identify syntax error in catch block

    The catch block must have parentheses around the error variable, so catch e is invalid syntax.
  2. Step 2: Correct the catch syntax

    Changing it to catch (e) fixes the syntax and allows error handling.
  3. Final Answer:

    Change catch e to catch (e) -> Option B
  4. Quick Check:

    Catch needs parentheses around error [OK]
Hint: Catch error variable must be in parentheses [OK]
Common Mistakes:
  • Omitting parentheses in catch
  • Adding finally when not needed
  • Ignoring undeclared variable error
5.

You want to safely parse JSON data that might be invalid. Which code snippet correctly catches parsing errors?

const jsonString = '{"name": "Alice", age: 25}';
try {
const data = JSON.parse(jsonString);
console.log(data.name);
} catch (error) {
console.log('Invalid JSON:', error.message);
}
hard
A. You must use if-else instead of try-catch for JSON parsing
B. This code will crash because JSON.parse never throws errors
C. The catch block should be after console.log(data.name)
D. This code correctly catches JSON parsing errors and logs a message

Solution

  1. Step 1: Understand JSON.parse behavior

    JSON.parse throws an error if the JSON string is invalid, like missing quotes around keys.
  2. Step 2: Check try-catch usage

    The try block runs JSON.parse, and the catch block handles any parsing error to avoid crashing.
  3. Final Answer:

    This code correctly catches JSON parsing errors and logs a message -> Option D
  4. Quick Check:

    try-catch handles JSON.parse errors [OK]
Hint: Use try-catch to handle JSON.parse errors safely [OK]
Common Mistakes:
  • Assuming JSON.parse never throws errors
  • Placing catch block incorrectly
  • Using if-else instead of try-catch for errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard