Bird
Raised Fist0
Javascriptprogramming~20 mins

Throwing errors in Javascript - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What does the throw statement do in JavaScript?
easy
A. Stops the program and creates an error.
B. Catches an error and handles it.
C. Declares a new variable.
D. Runs a function asynchronously.

Solution

  1. Step 1: Understand the purpose of throw

    The throw statement is used to create an error and stop normal program execution.
  2. Step 2: Differentiate from error handling

    Unlike try...catch which handles errors, throw actually creates the error.
  3. Final Answer:

    Stops the program and creates an error. -> Option A
  4. Quick Check:

    throw creates error = A [OK]
Hint: Remember: throw creates errors, try...catch handles them [OK]
Common Mistakes:
  • Confusing throw with try...catch
  • Thinking throw handles errors
  • Believing throw declares variables
2. Which of the following is the correct syntax to throw a new error with the message "Invalid input"?
easy
A. throw 'Invalid input';
B. throw error('Invalid input');
C. throw new Error('Invalid input');
D. throw new error('Invalid input');

Solution

  1. Step 1: Recall the correct Error constructor usage

    In JavaScript, to create an error object, use new Error('message').
  2. Step 2: Check syntax correctness

    Only throw new Error('Invalid input'); uses throw new Error('Invalid input'); which is correct syntax.
  3. Final Answer:

    throw new Error('Invalid input'); -> Option C
  4. Quick Check:

    Correct syntax uses new Error = C [OK]
Hint: Always use 'throw new Error' with capital E [OK]
Common Mistakes:
  • Missing 'new' keyword
  • Using lowercase 'error' instead of 'Error'
  • Omitting parentheses
3. What will be the output of the following code?
try {
  throw new Error('Oops!');
} catch (e) {
  console.log(e.message);
}
medium
A. Oops!
B. Error: Oops!
C. undefined
D. No output

Solution

  1. Step 1: Understand the throw and catch behavior

    The code throws an error with message 'Oops!'. The catch block catches it and logs e.message.
  2. Step 2: Determine what e.message contains

    e.message is the string 'Oops!'. So console.log(e.message) prints 'Oops!'.
  3. Final Answer:

    Oops! -> Option A
  4. Quick Check:

    Logging error message prints 'Oops!' = A [OK]
Hint: Catch error and log e.message to see the message [OK]
Common Mistakes:
  • Printing whole error object instead of message
  • Expecting 'Error: Oops!' instead of just message
  • Thinking no output occurs
4. Identify the error in this code snippet:
try {
  throw new Error('Fail');
} catch e {
  console.log(e.message);
}
medium
A. Incorrect use of throw keyword.
B. No error; code runs fine.
C. Error message string is invalid.
D. Missing parentheses around catch parameter.

Solution

  1. Step 1: Check catch syntax

    In JavaScript, the catch block must have parentheses around the error parameter, like catch (e).
  2. Step 2: Identify the syntax error

    The code uses catch e without parentheses, which causes a syntax error.
  3. Final Answer:

    Missing parentheses around catch parameter. -> Option D
  4. Quick Check:

    Catch parameter needs parentheses = B [OK]
Hint: Always write catch with parentheses: catch (e) [OK]
Common Mistakes:
  • Omitting parentheses in catch
  • Misplacing throw statement
  • Confusing error message with syntax error
5. You want to throw an error only if a user input is empty or null. Which code correctly throws an error in this case?
hard
A. if (input) throw new Error('Input required');
B. if (!input) throw new Error('Input required');
C. if (input == null) throw new error('Input required');
D. if (input === '') throw Error('Input required');

Solution

  1. Step 1: Understand falsy values for empty or null input

    In JavaScript, !input is true if input is null, undefined, empty string, 0, or false.
  2. Step 2: Check which option correctly throws error for empty or null

    if (!input) throw new Error('Input required'); uses if (!input) throw new Error('Input required'); which covers empty string and null correctly with proper syntax.
  3. Final Answer:

    if (!input) throw new Error('Input required'); -> Option B
  4. Quick Check:

    Use !input to check empty/null and throw Error = D [OK]
Hint: Use if (!input) to catch empty or null inputs [OK]
Common Mistakes:
  • Using lowercase 'error' instead of 'Error'
  • Throwing error only for empty string but missing null
  • Throwing error when input is valid
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