Skip to main content
101 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 ...
martin's user avatar
  • 12.7k
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 ...
sfdcfox's user avatar
  • 497k
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 ...
Aidan's user avatar
  • 13.8k
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 ...
crmprogdev's user avatar
16 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....
Keith C's user avatar
  • 136k
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. ...
AtulRajguru9's user avatar
  • 9,120
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 ...
Adrian Larson's user avatar
  • 151k
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 ...
Adrian Larson's user avatar
  • 151k
12 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 ...
Adrian Larson's user avatar
  • 151k
12 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 ...
Daniel Ballinger's user avatar
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 ...
Aidan's user avatar
  • 13.8k
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 ...
David Reed's user avatar
  • 93.2k
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 ...
sfdcfox's user avatar
  • 497k
9 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 ...
Adrian Larson's user avatar
  • 151k
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 ...
sfdcfox's user avatar
  • 497k
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 ...
sfdcfox's user avatar
  • 497k
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'...
sfdcfox's user avatar
  • 497k
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 { ...
Daniel Ballinger's user avatar
9 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 ...
Adrian Larson's user avatar
  • 151k
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 ...
Eric's user avatar
  • 54.2k
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:...
sfdcfox's user avatar
  • 497k
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 ...
sfdcfox's user avatar
  • 497k
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 ...
sfdcfox's user avatar
  • 497k
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 ...
Eric's user avatar
  • 54.2k
7 votes
Accepted

How to assert when return null

I highly recommend you never put assertions in a try/catch/finally block. Ever. You also shouldn't put them in if/else blocks. Here is a strategy I prefer for testing that an Exception should/shouldn'...
Adrian Larson's user avatar
  • 151k
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 ...
battery.cord's user avatar
  • 8,845
7 votes
Accepted

Exceptions and DML

The transaction will only be rolled back automatically if the thrown exception isn't caught. Your example showed a call stack like: Class A calls Class B Class B calls Class C Class C attempts a ...
Daniel Ballinger's user avatar
7 votes
Accepted

What are "LOB fields" in an asset? Querying all the fields on the asset causes a limit exception, why?

LOB simply means "Large Object" fields. They refer to long text areas (those greater than 255 characters), be it standard or custom, such as Description or MyCustomLongText__c. Note that heap isn't ...
sfdcfox's user avatar
  • 497k
7 votes
Accepted

Is it Possible to Catch an Unknown Exception?

No, UnexpectedException, AssertException, and LimitException are all uncatchable. Easy enough to verify via Execute Anonymous: try { throw new UnexpectedException(); } catch(UnexpectedException ...
Adrian Larson's user avatar
  • 151k
7 votes
Accepted

Do we really need to wait up to 24 hours for Apex Exception Emails?

The standard mechanism for sending emails from unhandled exceptions is not reliable, see this question: When are unhandled exception emails suppressed and not sent? or Why do I not always get ...
Aidan's user avatar
  • 13.8k

Only top scored, non community-wiki answers of a minimum length are eligible