84 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 ...
user avatar
  • 12.2k
68 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 ...
user avatar
  • 429k
57 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-...
user avatar
  • 142k
27 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 ...
user avatar
  • 12.7k
24 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 ...
user avatar
  • 543
23 votes
Accepted

How can I cause side effects outside an execution context?

Update, Summer '17: Platform Events are now GA. Note the following from the docs: Platform Events and Transactions Unlike custom objects, platform events aren’t processed within database ...
user avatar
23 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 ...
user avatar
  • 429k
18 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 ...
user avatar
  • 40.3k
15 votes

How can I cause side effects outside an execution context?

I wrote a bit on my blog last week about how I was using the Tooling api to effectively create an "eval()" method. You could replace the transaction mechanism with a similar eval mechanism. The "eval'...
user avatar
  • 6,968
14 votes
Accepted

How to insert a record and then do callout?

Please check this link it may help: https://help.salesforce.com/apex/HTViewSolution?id=000003701 You have uncommitted work pending. Please commit or rollback before calling out" Description ...
user avatar
  • 479
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. ...
user avatar
  • 8,979
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 ...
user avatar
  • 142k
11 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 ...
user avatar
  • 142k
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 ...
user avatar
  • 12.7k
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 ...
user avatar
  • 85.9k
10 votes
Accepted

Check APEX exception type

There is a getTypeName() method on the Exception class that you can use. It will output the actual instance's type. Example 1: try { //SOME LOGIC } catch (Exception ex) { System.debug(ex....
user avatar
  • 28.6k
10 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 ...
user avatar
9 votes
Accepted

Catch System.AssertException

Short answer no. From the Apex Developers Guide: Asserts that condition is true. If it is not, a fatal error is returned that causes code execution to halt. The returned error optionally ...
user avatar
  • 19.7k
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(...
user avatar
  • 142k
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 [...] ...
user avatar
  • 2,012
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 ...
user avatar
  • 429k
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 ...
user avatar
  • 429k
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 ...
user avatar
  • 429k
9 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 ...
user avatar
  • 142k
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 ...
user avatar
  • 53.1k
8 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'...
user avatar
  • 429k
8 votes
Accepted

Apex InvalidParameterValueException class constructor parameters?

It's Java, not Apex, but here is some documentation on a version of InvalidParameterValueException which suggests the first argument is the name of the parameter, and the second is its value: Field ...
user avatar
  • 142k
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:...
user avatar
  • 429k
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 ...
user avatar
  • 429k
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 ...
user avatar
  • 429k

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