Bird
0
0

Which of the following is the correct syntax to throw a custom exception named MyException?

easy📝 Syntax Q12 of 15
Java - Custom Exceptions
Which of the following is the correct syntax to throw a custom exception named MyException?
Athrow new MyException();
Bthrow MyException();
Cthrow exception MyException();
Dthrow exception new MyException();
Step-by-Step Solution
Solution:
  1. Step 1: Recall Java syntax for throwing exceptions

    In Java, to throw an exception, you use the keyword throw followed by new and the exception class constructor.
  2. Step 2: Match the syntax with the options

    Only throw new MyException(); uses the correct syntax: throw new MyException();
  3. Final Answer:

    throw new MyException(); -> Option A
  4. Quick Check:

    Throw syntax = throw new Exception() [OK]
Quick Trick: Always use 'throw new ExceptionName()' to throw exceptions [OK]
Common Mistakes:
  • Omitting the 'new' keyword
  • Adding extra keywords like 'exception'
  • Using parentheses without 'new'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes