PDI · Question #228
A developer creates a custom exception as shown below: public class ParityException extends Exception {} What are two ways the developer can fire the exception in Apex? Choose 2 answers
The correct answer is B. throw new ParityException("parity does not match"); D. throw new ParityException(). The question asks for the correct Apex syntax to instantiate and explicitly throw a custom exception.
Question
A developer creates a custom exception as shown below:
public class ParityException extends Exception {} What are two ways the developer can fire the exception in Apex? Choose 2 answers
Options
- Anew ParityException();:
- Bthrow new ParityException("parity does not match");
- Cnew ParityException('parity does not match');
- Dthrow new ParityException();
How the community answered
(51 responses)- A4% (2)
- B94% (48)
- C2% (1)
Why each option
The question asks for the correct Apex syntax to instantiate and explicitly throw a custom exception.
Instantiating an exception with `new ParityException();` only creates an object but does not fire or propagate the exception without the `throw` keyword.
The `throw` keyword is essential to fire an exception, and `new ParityException("parity does not match")` correctly instantiates the custom exception with a message parameter.
Instantiating an exception with `new ParityException('parity does not match');` only creates an object with a message but does not fire or propagate the exception without the `throw` keyword.
The `throw` keyword is essential to fire an exception, and `new ParityException()` correctly instantiates the custom exception without a specific message, using its default constructor.
Concept tested: Throwing custom Apex exceptions
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_creating_exceptions.htm
Topics
Community Discussion
No community discussion yet for this question.