CRT-450 practice question 15 of 30

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?

Full set: 201 questions
Answer options
A: throw new ParityException (); Correct answer
B: throw new parityException ('parity does not match'); Correct answer
C: new ParityException ();
D: new ParityException('parity does not match');
Correct Answer:
A. throw new ParityException ();; B. throw new parityException ('parity does not match');
Explanation

In Apex, "firing" or "raising" an exception is accomplished using the throw statement. This statement interrupts the normal execution flow and passes an instantiated exception object to the runtime. Options A and B both correctly use the throw keyword followed by the instantiation of the ParityException object using the new keyword. Although the custom ParityException class does not explicitly define constructors, it inherits them from the base Exception class. This includes a no-argument constructor (used in A) and a constructor that accepts a String message (used in B). Apex is case-insensitive for class and variable names, so parityException resolves to ParityException.

References

1. Salesforce Apex Developer Guide

"Exception Handling": The guide explicitly states

"To fire an exception

use the throw statement." This confirms that the throw keyword is mandatory for the action described in the question.

2. Salesforce Apex Developer Guide

"Using Custom Exceptions": This section provides examples that demonstrate the correct syntax for firing a custom exception

which is throw new MyException('Error message');. This directly supports the patterns in options A and B.

3. Salesforce Apex Developer Guide

"Exception Class and Built-In Exceptions": This documentation details the base Exception class

from which all other exceptions inherit. It documents the available constructors

including Exception() and Exception(message)

which are inherited by the custom ParityException class

validating the instantiation methods in both A and B.

4. Salesforce Apex Developer Guide

"Apex Language Constructs"

"Case Sensitivity": This section clarifies that "Apex is case-insensitive

" which validates that parityException in option B is syntactically correct and will be treated the same as ParityException.

Scroll to Top