What is a Custom Exception?
In Apex, an exception is an error condition that can be thrown to interrupt normal program flow. By default, Salesforce provides common exceptions like DmlException
, QueryException
, etc. However, sometimes you may want to define your own exception types that are specific to your application’s business logic. This is where custom exceptions come in.
A custom exception extends the Exception
class and allows you to define your own error message, and optionally, methods to manipulate or log information specific to the error.
Why Use Custom Exceptions?
- Separation of Concerns: Custom exceptions let you define specific error cases that your code handles, isolating errors for easy troubleshooting.
- Meaningful Error Messages: You can include custom messages that provide more context about what went wrong.
- Clearer Logic: With custom exceptions, you can add specific logic that handles different error types in a precise manner.
Basic Syntax:
To define a custom exception, you create a class that extends the Exception
class, like so:
Once the exception is defined, you can throw it using throw new MyCustomException('Error message')
, and it can be caught in a try-catch
block.
Example 1: Simple Custom Exception
Custom Exception Class
Apex Class with Custom Exception
Test Class for Custom Exception
In the test class, you can simulate an error condition that will trigger the exception.
Example 2: Custom Exception with Additional Properties
You can enhance custom exceptions by adding properties or methods to carry additional context information.
Custom Exception with Additional Properties
Apex Class Using This Exception
Test Class for the Enhanced Custom Exception
Example 3: Custom Exception with Logging
In some cases, you may want to log the exception details to a custom object for auditing purposes. You can include such logic in your custom exception class.
Custom Exception with Logging
Apex Class Using This Exception
Test Class for the Logging Custom Exception
Example 4: Custom Exception with Multiple Catch Blocks
If your application needs to handle multiple types of exceptions (e.g., DmlException
, QueryException
, etc.), you can create several custom exceptions to handle specific cases.
Custom Exception for DML Errors
Apex Code Handling DML Exceptions
Test Class for DML Exception
Summary of Key Benefits of Custom Exceptions:
- Clarity in Error Handling: Custom exceptions provide more meaningful error messages specific to your business logic.
- Contextual Information: You can pass additional context (e.g., query, balance) with custom exceptions.
- Custom Handling: You can add logic to handle exceptions more gracefully, like logging, custom alerts, etc.
- Better Maintainability: By naming exceptions appropriately, you make the code easier to maintain and debug.
By using custom exceptions, you can write more efficient, understandable, and maintainable Apex code. They help ensure that specific errors are caught and handled in a way that is relevant to your application’s needs.
No comments:
Post a Comment