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 ...
martin's user avatar
  • 12.6k
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
  • 481k
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-...
Adrian Larson's user avatar
  • 149k
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.5k
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 ...
sfdcfox's user avatar
  • 481k
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 ...
sskular's user avatar
  • 573
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
  • 40.9k
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....
Keith C's user avatar
  • 135k
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,110
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
  • 149k
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
  • 149k
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 ...
Adrian Larson's user avatar
  • 149k
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.5k
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 ...
Daniel Ballinger's user avatar
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
  • 92.1k
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(...
Adrian Larson's user avatar
  • 149k
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 [...] ...
abd3721's user avatar
  • 2,132
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
  • 481k
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
  • 481k
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
  • 481k
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
  • 481k
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
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 ...
Adrian Larson's user avatar
  • 149k
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
  • 54k
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 ...
Adrian Larson's user avatar
  • 149k
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
  • 481k
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
  • 481k
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
  • 481k
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
  • 54k
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,767

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