nerdexam
Salesforce

PDI · Question #238

Considering the following code snippet: When the code executes a DML exception is thrown. How should the developer modify the code to ensure exceptions are handled gracefully?

The correct answer is A. Implement a try/catch block for the DML. To gracefully handle DML exceptions that are thrown during code execution, a developer should implement a try/catch block around the DML statement.

Submitted by layla.eg· Apr 18, 2026Logic and Process Automation

Question

Considering the following code snippet:

When the code executes a DML exception is thrown. How should the developer modify the code to ensure exceptions are handled gracefully?

Exhibit

PDI question #238 exhibit

Options

  • AImplement a try/catch block for the DML.
  • BRemove null items from the list if Accounts.
  • CImplement the upsert DML statement.
  • DImplement Change Data Capture

How the community answered

(49 responses)
  • A
    88% (43)
  • B
    2% (1)
  • C
    2% (1)
  • D
    8% (4)

Why each option

To gracefully handle DML exceptions that are thrown during code execution, a developer should implement a try/catch block around the DML statement.

AImplement a try/catch block for the DML.Correct

A try/catch block is the standard and most effective programming construct in Apex for gracefully catching and managing runtime exceptions, including those thrown during DML operations, allowing the developer to control program flow and provide appropriate error responses.

BRemove null items from the list if Accounts.

While removing null items could prevent specific DML errors related to required fields, it is not a comprehensive or general solution for gracefully handling all potential DML exceptions.

CImplement the upsert DML statement.

The upsert DML statement prevents certain types of DML errors (like duplicate records based on external ID) but does not provide a general mechanism for gracefully handling all DML exceptions.

DImplement Change Data Capture

Change Data Capture is a feature for asynchronously processing record changes, not for handling synchronous DML exceptions within executing code.

Concept tested: Apex DML exception handling

Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_handling.htm

Topics

#DML exception handling#try/catch#Apex programming#Error management

Community Discussion

No community discussion yet for this question.

Full PDI Practice