95
votes
Accepted
Testing AuraHandledExceptions?
The problem is that the constructor for the AuraHandledException only sets the string argument to be the message sent to the client-end, while calling getMessage() on the exception through apex ...
71
votes
Accepted
Pokemon Catch - Generic Exception Handling In Apex
Pros
One of the primary advantages of the Pokemon Catch is that you reduce the complexity of code in many cases. For example, if you're concerned about a NullPointerException, a CalloutException, a ...
66
votes
Accepted
Can Built-In Exceptions Be Thrown?
As far as I know, @mattandneil was the first to discover this change in this answer to a question by @DanielBallinger.
Despite the documentation's claims to the contrary, the compiler now allows built-...
31
votes
Pokemon Catch - Generic Exception Handling In Apex
In Java, I'd agree with the sfdcfox's takeaway. But (particularly in the absence of documentation about which Exceptions Apex will throw), catching Exception can make a lot of sense. I tend to go ...
27
votes
Accepted
JSON: Cannot deserialize instance of date from VALUE_STRING
Three problems here:
(a) Date receiving DateTime value.
The value should be "YYYY-MM-DD" only.
(b) Decimal receiving String value.
The value should not be in quotes.
(c) You must not have a ...
25
votes
Accepted
Check if a record is in approval process
From Winter16 you can check this using the Approval system class
Pass it the Id of a record you want to check if is locked or not:
Approval.isLocked(recordId);
Make sure you set the class api version ...
19
votes
Pokemon Catch - Generic Exception Handling In Apex
I realize I'm a "late comer" to answering this question, but it was closed by the time I saw it initially. Now that it's been reopened, I feel I have something important to add that's not been given ...
15
votes
Accepted
throwing aurahandledexception is not sending message correctly to LWC
I recollect that this is a design quirk of AuraHandledException and that e.g. this is needed (constructor argument probably doesn't matter):
AuraHandledException e = new AuraHandledException(System....
14
votes
Pokemon Catch - Generic Exception Handling In Apex
Here are my views on this:
Is it ever acceptable (or even preferable) to catch a generic Exception in Apex?
Yes, there are definitely situations where a generic Exception is acceptable or preferred.
...
13
votes
Test Class for Catch Exception getMessage
You should really know a more specific type of Exception. If you are working out how to get the try block to throw an exception at all, see the bottom section.
If you know calling a method will cause ...
12
votes
Accepted
deserializeStrict method: bug?
Aside from that last sentence the documentation is spot on. However, I would argue that the behavior is correct, and it is just that sentence which is flawed.
It is easy to demonstrate that for a ...
11
votes
Accepted
What is an elegant way to catch multiple exceptions without catching generic exceptions?
You can still support concrete types with getTypeName by using the Type.forName method.
Set<Type> allowlist = new Set<Type> { DmlException.class, ListException.class };
try
{
// do ...
11
votes
Get StackTrace in Custom Exception
You can actually work around the problem of custom exceptions failing to provide stacktraces...
Here's an abstract class that you can extend instead of Exception itself:
public abstract class ...
11
votes
Accepted
When are unhandled exception emails suppressed and not sent? or Why do I not always get exception emails even though I've set them up?
Found this interesting statement in the idea Need a reliable developer script exception alert mechanism:
Salesforce R&D has acknowledged the issue but states it is a feature frozen area ...
11
votes
Catching an UnexpectedException?
System.UnexpectedException gets thrown a lot with Note inserts if you do not prepare the content exactly right, and they cannot be caught or handled in any way.
I wrote an open source package a while ...
9
votes
Accepted
Apex strange exception handling
Exception Cause
The key issue that is causing that exception is that you are trying to assign a Group Id to a User Lookup. You need to make sure it is a User Id first.
if (obj.OwnerId.getSObjectType(...
9
votes
Accepted
Why do I get a DisjunctionsNotSupportedException when doing a SOQL query on Custom Metadata?
Custom Metadata supports a limited SOQL syntax, and "OR" statements are not supported. From the documentation:
Custom metadata types support the following SOQL query syntax.
SELECT fieldList [...]
...
9
votes
List of all possible Apex Exceptions
There's two categories of exceptions: normal exceptions and DML exceptions. The Exception class outlines all the possible code exceptions you can expect, with short descriptions of each, while Core ...
9
votes
Accepted
Handling exception using custom exception
Catch allows you to catch a specific type of exception, such as a DmlException, a QueryException, or a SuperUncoolException. When you create a new Exception type, you're defining a new type of ...
9
votes
Accepted
How can exceptions be handled in Apex without using try/catch?
There are two ways to handle exceptions in Apex Code: reactive or preemptive. In reactive model of exception handling, we react to an exception, by using try-catch. In the preemptive model of ...
9
votes
Accepted
PB + Queueable System.FinalException: Record is read-only
I suspect that you've unwittingly copied the "isReadOnly" flag on the sObject records. This flag, which can't be accessed or modified directly, is responsible for causing the FinalException that you'...
9
votes
Accepted
Get StackTrace in Custom Exception
Update - the known issue was resolved in the Spring `22 release.
Both debug statements in the following anonymous Apex example will produce the stack trace.
class myexception extends exception{}
try {
...
8
votes
Accepted
Exception class constructor
There are also Exception constructors that accept another Exception. Take a look at Creating Custom Exceptions, which shows constructors you can define on a class that simply extends Exception without ...
8
votes
Accepted
FIELD_CUSTOM_VALIDATION_EXCEPTION, This record is locked from editing
A user with appropriate permissions can modify a record locked by an approval process.
The error you are getting is due to a validation rule and not the approval process. If it was due to the ...
8
votes
Accepted
Apex InvalidParameterValueException class constructor parameters?
It's Java, not Apex, but here was some documentation (since removed) on a version of InvalidParameterValueException which suggests the first argument is the name of the parameter, and the second is ...
8
votes
Accepted
Try, try, catch, catch
Typically, you would use try-catch inside another because you don't want to exit the entire try-catch block because of a (possibly expected) error. For example, I once wrote code that looked like this:...
8
votes
Not able to catch an exception (Custom + Standard) from trigger
Try-catch spanning trigger transactions has been exceptionally unreliable for about a year now (if not longer). Some time ago, you used to be able to catch the correct type of exception (e.g. a ...
8
votes
Accepted
Is it fine to create a custom exception class that has the same name as a built in one in apex?
You can, except that if you do so, if any other code anywhere in your code base references that exception, you may get unexpected results. For example, consider:
public class DmlException extends ...
7
votes
Accepted
Sandbox org TLS 1.1 update breaks outbound calls
Unfortunately it is up to the external service to negotiate the connection and support tls 1.1
The general compatibility guidelines from the API (inbound)
integrations (see section above) can be ...
7
votes
Get StackTrace in Custom Exception
You can access inherited variables and methods using the super keyword in an override method. Since you've inherited an exception, you have access to getStackTraceString() and getCause() inside ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
exception × 566apex × 325
trigger × 61
unit-test × 55
visualforce × 43
callout × 36
soql × 32
batch × 28
dml × 27
error × 26
dmlexception × 26
email × 23
try-catch × 22
null-pointer × 20
governorlimits × 16
managed-package × 14
query × 14
lightning-aura-components × 12
lightning-web-components × 12
validation × 12
future × 10
controller × 9
error-messages × 9
unexpectedexception × 9
visual-workflow × 8