Skip to main content

Exception Handling

Explore practical guides and implementation patterns for this topic.

Posts

Lightning Web Components

Salesforce Exception Handling & Logging: Best Practices & Patterns

💬 In plain words: Let exceptions bubble up to one central place. Catch them there, and permanently write the error to a custom Log__c record via a Platform Event so the log survives a database rollback. An empty catch block is how errors vanish without a trace—and haunt you in production. 📌 Real-Life Example: The Silent Integration Failure Imagine an integration that fails silently for a week because someone wrote catch(Exception e){} . To fix this, catch the error once at the top level and publish an Error_Event__e carrying the message and record ID. A subscriber then inserts an Integration_Log__c . Now, even if the main transaction rolls back, the rollback can't erase the evidence.
Read article
Topic Apex
Apex Development

How to Prevent & Handle Apex Null Pointer Exceptions: Safe Navigation & Best Practices

In plain words: A Null Pointer Exception (NPE) happens when your code tries to open an empty box. If a variable or object points to null (nothing) and you attempt to read its fields, call its methods, or get its list items, Apex crashes because there is no data in memory to work with. In Salesforce development, the System.NullPointerException is one of the most frequent runtime runtime errors. Because Apex is strongly typed but initializes unassigned object references to null , any attempt to traverse unpopulated relationships, empty map lookups, or unassigned variables will throw an unhandled exception. Writing defensive, null-safe Apex keeps your applications stable and prevents unexpected transaction rollbacks.
Read article
Topic Apex Best Practices Custom Exceptions Error Logging Exception Handling Salesforce Apex Salesforce Development Try-Catch
Apex Best Practices Custom Exceptions Error Logging Exception Handling Salesforce Apex Salesforce Development Try-Catch

Apex Exception Handling: Complete Guide to Try-Catch, Custom Exceptions, & Best Practices

In plain words: Think of an exception as a surprise roadblock during code execution. Exception handling is your detour plan—using try-catch blocks to catch unexpected errors, log what went wrong, and keep your application running without crashing the user interface. In Salesforce development, writing defensive Apex code means planning for real-world failures: failed validation rules, missing records, locked database rows, and network timeouts. Implementing structured exception handling ensures that unexpected errors fail gracefully, preserve data integrity, and provide actionable feedback to users and developers.
Read article
Topic Apex Architecture Database Methods DML Exceptions Exception Handling MixedDmlException Salesforce Apex Salesforce Development
Apex Architecture Database Methods DML Exceptions Exception Handling MixedDmlException Salesforce Apex Salesforce Development

Handling Apex DML Exceptions in Salesforce: Patterns, Best Practices, and Code Examples

In plain words: DML (Data Manipulation Language) is how Apex creates, updates, and deletes records in Salesforce. DML exceptions occur when database operations fail—such as hitting a custom validation rule, violating required fields, or mixing setup and non-setup records in the same transaction. Every Salesforce developer writes DML statements like insert , update , and delete . However, unexpected database rejections, permission restrictions, and governor limit boundaries can halt execution immediately if not handled defensively. Understanding the specific exceptions thrown during DML operations enables you to prevent transaction rollbacks and deliver actionable feedback to your users.
Read article
Topic Apex Debugging Best Practices Debug Logs Developer Console Exception Handling Governor Limits Salesforce Development
Apex Debugging Best Practices Debug Logs Developer Console Exception Handling Governor Limits Salesforce Development

Master Salesforce Debugging: Techniques, Tools & Best Practices Guide

In plain words: Salesforce Debugging is the process of tracking execution paths, inspecting variable states, and diagnosing errors in Apex code, triggers, and automated flows. By mastering diagnostic tools like debug logs, the Developer Console, and the Apex Debugger, developers can isolate bugs and fix performance issues quickly. Debugging is an essential skill for developers working in Salesforce. It helps identify and resolve issues in code, understand the flow of execution, and optimize performance. Whether you are troubleshooting unexpected test failures or tracking down production errors, knowing how to navigate Salesforce logging frameworks is critical for maintaining robust applications.
Read article