Bird
0
0

Which of the following is the correct way to throw a NullPointerException with a custom message in Java?

easy📝 Syntax Q3 of 15
Java - Exception Handling

Which of the following is the correct way to throw a NullPointerException with a custom message in Java?

Athrow new Exception("NullPointerException");
Bthrow NullPointerException("Null value found");
Cthrow new NullPointerException("Null value found");
Dthrow NullPointerException();
Step-by-Step Solution
Solution:
  1. Step 1: Syntax for throwing exceptions

    Use throw new ExceptionType("message") to throw an exception with a message.
  2. Step 2: Identify correct syntax

    throw new NullPointerException("Null value found"); correctly creates a new NullPointerException object with a message and throws it.
  3. Final Answer:

    throw new NullPointerException("Null value found"); -> Option C
  4. Quick Check:

    Check for new keyword and parentheses [OK]
Quick Trick: Always use 'throw new ExceptionType()' syntax [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Throwing generic Exception instead
  • Calling exception like a method without 'new'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes