Bird
Raised Fist0
Javascriptprogramming~20 mins

Try–catch block in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Try–catch Master
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 printed to the console when this code runs?
Javascript
try {
  console.log('Start');
  throw new Error('Oops!');
  console.log('End');
} catch (e) {
  console.log('Caught:', e.message);
}
ACaught: Oops!
BStart\nEnd\nCaught: Oops!
CStart\nCaught: Oops!
DStart\nEnd
Attempts:
2 left
💡 Hint
Remember that code after throw inside try does not run.
❓ Predict Output
intermediate
2:00remaining
Value of variable after try–catch
What is the value of variable x after this code runs?
Javascript
let x = 0;
try {
  x = 5;
  throw 'error';
  x = 10;
} catch (e) {
  x = 20;
}
A20
B5
C10
D0
Attempts:
2 left
💡 Hint
Check which assignments run before and after the throw.
❓ Predict Output
advanced
2:30remaining
Output with nested try–catch and finally
What will this code print to the console?
Javascript
try {
  console.log('Outer try');
  try {
    console.log('Inner try');
    throw 'error';
  } catch (e) {
    console.log('Inner catch');
    throw 'new error';
  } finally {
    console.log('Inner finally');
  }
} catch (e) {
  console.log('Outer catch');
} finally {
  console.log('Outer finally');
}
AOuter try\nInner try\nInner catch\nOuter catch\nOuter finally
BOuter try\nInner try\nInner catch\nOuter catch\nInner finally\nOuter finally
COuter try\nInner try\nInner finally\nInner catch\nOuter catch\nOuter finally
DOuter try\nInner try\nInner catch\nInner finally\nOuter catch\nOuter finally
Attempts:
2 left
💡 Hint
Remember finally always runs after try or catch blocks.
❓ Predict Output
advanced
2:00remaining
Error type caught in catch block
What will be the output of this code?
Javascript
try {
  JSON.parse('invalid json');
} catch (e) {
  console.log(e instanceof SyntaxError);
  console.log(e instanceof Error);
  console.log(e.name);
}
Afalse\ntrue\nSyntaxError
Btrue\ntrue\nSyntaxError
Ctrue\nfalse\nError
Dfalse\nfalse\nError
Attempts:
2 left
💡 Hint
Check the type of error JSON.parse throws on invalid input.
🧠 Conceptual
expert
2:30remaining
Behavior of try–catch with asynchronous code
Consider this code snippet. Which statement is true about the output?
Javascript
try {
  setTimeout(() => {
    throw new Error('Async error');
  }, 0);
} catch (e) {
  console.log('Caught:', e.message);
}
console.log('End');
APrints only 'End' and the error is uncaught
BPrints 'Caught: Async error' then 'End'
CThrows SyntaxError before printing anything
DPrints 'End' then 'Caught: Async error'
Attempts:
2 left
💡 Hint
Remember how try–catch works with asynchronous callbacks.

Practice

(1/5)
1.

What is the main purpose of a try block in JavaScript?

easy
A. To run code that might cause an error
B. To declare variables
C. To create loops
D. To define functions

Solution

  1. Step 1: Understand the role of try

    The try block is used to run code that might throw an error during execution.
  2. Step 2: Differentiate from other code blocks

    Other options like declaring variables, loops, or functions are unrelated to error handling.
  3. Final Answer:

    To run code that might cause an error -> Option A
  4. Quick Check:

    try runs risky code [OK]
Hint: Try block runs risky code safely [OK]
Common Mistakes:
  • Thinking try declares variables
  • Confusing try with loops
  • Assuming try defines functions
2.

Which of the following is the correct syntax to start a try-catch block in JavaScript?

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

Solution

  1. Step 1: Recall correct try-catch syntax

    The correct syntax uses curly braces and parentheses for catch: try { ... } catch (error) { ... }.
  2. Step 2: Identify syntax errors in other options

    Options B, C, and D misuse colons, parentheses, or omit parentheses around the error parameter.
  3. Final Answer:

    try { /* code */ } catch (error) { /* handle error */ } -> Option B
  4. Quick Check:

    Curly braces and catch(error) needed [OK]
Hint: Catch needs parentheses around error [OK]
Common Mistakes:
  • Using colons instead of braces
  • Omitting parentheses in catch
  • Using parentheses after try
3.

What will be the output of the following code?

try {
  console.log('Start');
  throw new Error('Oops');
  console.log('End');
} catch (e) {
  console.log('Caught:', e.message);
}
medium
A. Start\nEnd
B. Start
C. Caught: Oops
D. Start\nCaught: Oops

Solution

  1. Step 1: Trace code inside try block

    The first console logs 'Start'. Then an error is thrown, so the next console.log('End') is skipped.
  2. Step 2: Catch block runs after error

    The catch block catches the error and logs 'Caught: Oops'.
  3. Final Answer:

    Start\nCaught: Oops -> Option D
  4. Quick Check:

    Error stops try, catch logs message [OK]
Hint: Error stops try; catch runs next [OK]
Common Mistakes:
  • Thinking code after throw runs
  • Ignoring catch block output
  • Confusing error message with error object
4.

Find the error in this code snippet:

try {
  console.log('Hello');
} catch e {
  console.log('Error:', e);
}
medium
A. Missing parentheses around e in catch
B. Missing semicolon after console.log
C. Try block cannot have console.log
D. Catch block must come before try

Solution

  1. Step 1: Check catch syntax

    The catch block must have parentheses around the error variable: catch (e).
  2. Step 2: Verify other parts

    Semicolons are optional in JavaScript; console.log is allowed in try; catch must come after try.
  3. Final Answer:

    Missing parentheses around e in catch -> Option A
  4. Quick Check:

    Catch needs (error) [OK]
Hint: Catch always needs parentheses around error [OK]
Common Mistakes:
  • Omitting parentheses in catch
  • Thinking semicolons are mandatory here
  • Reversing try and catch order
5.

You want to safely parse a JSON string that might be invalid. Which code correctly uses try-catch to handle errors and return null if parsing fails?

?
hard
A. try { JSON.parse(str); } catch { return null; }
B. try { return JSON.parse(str); } catch e { return null; }
C. try { return JSON.parse(str); } catch (e) { return null; }
D. try { return JSON.parse(str); } catch (e) { throw null; }

Solution

  1. Step 1: Understand JSON parsing risks

    Parsing invalid JSON throws an error, so we use try-catch to handle it safely.
  2. Step 2: Check correct catch syntax and behavior

    try { return JSON.parse(str); } catch (e) { return null; } uses correct catch syntax with (e) and returns null on error, which is the goal.
  3. Step 3: Identify errors in other options

    try { JSON.parse(str); } catch { return null; } misses return before JSON.parse(str), so returns undefined on success. try { return JSON.parse(str); } catch e { return null; } has syntax error (catch e needs catch (e)). try { return JSON.parse(str); } catch (e) { throw null; } throws null instead of returning it.
  4. Final Answer:

    try { return JSON.parse(str); } catch (e) { return null; } -> Option C
  5. Quick Check:

    Catch error with (e), return null on failure [OK]
Hint: Catch error with (e), return null on failure [OK]
Common Mistakes:
  • Omitting parentheses in catch
  • Throwing instead of returning null
  • Assuming catch without parameter always works
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