Courseiva

SF-PD2 Testing, Debugging, and Deployment Practice Question

A developer is creating a custom exception to handle business logic errors. What is the correct way to declare this exception so it can be caught specifically in a try-catch block?

⚠ Common exam trap

Candidates often mistakenly believe custom exceptions must extend 'System.Exception' or 'ApexException' rather than the standard 'Exception' class, which is the actual base class for all custom exceptions in Apex.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

public class MyException extends Exception {}

Defining a custom exception class extending the built-in 'Exception' class is the standard practice in Apex. This allows developers to catch specific business errors separately from system errors like NullPointerExceptions. Proper exception handling is critical for maintaining clean, readable code and ensuring that users receive meaningful feedback when operations fail during complex business processes or integration calls.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    public class MyException implements Exception {}

    Why it's wrong here

    In Apex, custom exceptions must inherit from the base Exception class using the 'extends' keyword. Using 'implements' is incorrect as the Exception type is a class, not an interface. Attempting to implement it will cause a compilation error. Proper inheritance is required to gain standard exception behaviors like message propagation.

  • ✓

    public class MyException extends Exception {}

    Why this is correct

    Extending the base Exception class is the correct way to create a custom exception in Apex. This allows the class to inherit all core exception functionality, including tracking the stack trace and message strings, while enabling specific catch blocks that can handle these errors distinctly from system-level platform exceptions.

  • ✗

    public exception MyException {}

    Why it's wrong here

    There is no 'exception' keyword in Apex for declaring types. All custom exceptions must be declared as a 'class' that extends the base Exception class. Using invalid keywords like 'exception' as a type modifier results in a compilation error. Developers must follow the class-inheritance pattern for custom error handling structures.

  • ✗

    public static exception MyException = new Exception();

    Why it's wrong here

    Exceptions are types, not instances of variables. Declaring an exception as a static variable is syntactically and logically incorrect in Apex. Custom exceptions must be defined as separate classes. This approach ensures proper type scoping and allows the developer to throw new instances of the custom error as needed.

About these practice questions

Courseiva writes every SF-PD2 question from scratch — 226 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Salesforce exam blueprint

This SF-PD2 practice question is part of Courseiva's free Salesforce certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the SF-PD2 exam.