Sometimes in code examples of custom save methods (of controller extensions) you see the use of try/catch and an explicit translation of Exception to PageMessage.

Explicit Error Handling:

public PageReference save() {
    try {
        update record;
    catch(Exception ex) {
        return null;

    return new PageReference('/' + record.Id);

Is that really needed or is this short version doing the same? With same I mean in case of an exception null is returned and the error is shown in the page message?

Implicit error Handling:

public PageReference save() {
    update record;
    return new PageReference('/' + record.Id);
  • 1
    Side note, I recommend putting the "return" in the "finally" section of the try... – Nathan Williams Mar 18 '15 at 14:48
  • @NathanWilliams. Agreed. That makes much more sense to me as well. – crmprogdev Mar 18 '15 at 15:57

They are not the same.

First scenario

Error is thrown, but caught by the try catch. You are then adding the error to the page by using the ApexPages.addMessage() function. This is a better experience for the end user since the error will show at the top of the current page they are on.

Second Scenario

Error is encountered and user will be redirected immediately to a white page with an error message.

Hope this helps!

Luis Luciani


I agree that the try/catch solution is better as the error is displayed nicely.

But this solution has a big drawback: If you try/catch DMLExceptions the platform is not able to autpo-rollback changes and might compromise ACID-properties.

  • If this is just one DML on one record, won't it either succeed or fail? It doesn't seem like you need to be worried about rollback unless you had multiple DML operations in your method. – Jeremy Nottingham Mar 19 '15 at 15:41
  • You are right. But if inside the try/catch there would be multiple DML statements my point would be valid. – Robert Sösemann Mar 19 '15 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.