+ Start a Discussion
KrisztianKrisztian 

Try catch and developer script exception

The organization owner received a developer script exception via email from the production. Please note that I have not access to the production, the owner deployed the change set.

There is a /JSON/ REST integration with an Apex class to receive POST callouts via a Site Guest User. The method throws an Exception (Upsert failed, DUPLICATE_VALUE) and this is correct. A value duplicated in a unique field.

The problem is something else: The full method body is in a try-catch. The line (line 82) that causes the exception is in the try block. Why received the owner the developer script exception email? The salesforce send developer script exception email from handled exception?

Subject: Developer script exception from Természetesen Alapítvány : AccountForContactCreation : AccountForContactCreation: execution of BeforeInsert caused by: System.DmlException: Upsert failed. First exception on row 102; first error: DUPLICATE_VALUE, Duplicate external id specified: XXXXXXXXXXXXXXX: [A_NameExternalId__c] Trigger.AccountForContactCreation: line 82, column 1
To: "info@XXXXXXXXXX.com" <info@XXXXXXXXXX.com>


Apex script unhandled trigger exception by user/organization: 0050O00000XXXX/00D2000000XXXXX

AccountForContactCreation: execution of BeforeInsert

caused by: System.DmlException: Upsert failed. First exception on row 102; first error: DUPLICATE_VALUE, Duplicate external id specified: XXXXXXXXXXXXXXX: [A_NameExternalId__c]

Trigger.AccountForContactCreation: line 82, column 1

Thanks in advance,
Krisztian

Alain CabonAlain Cabon
Hi,

This user has deployed the program that causes the error or the email of the technical user who has deployed is associated with a group and this user belongs to this group. 
 
Om PrakashOm Prakash
Hi,
Is it possible that there are try catch in code but exceptions are not handled for DMLException or Exception.
May be some other specific exceptions like NullPointerException or CalloutException are mentioned there.
Salesforce send “Developer script exception” on email only in case it was unhandled.
KrisztianKrisztian
Hi Aladin, 
The administrator user deployed the change set, but the (technical) site guest user caused the exception.
KrisztianKrisztian
Hi Om,
I am sorry, could you detail it, please?
try {
    // causes System.DMLException
} catch(Exception ex) {
}
I think it have to catch the DMLException based on https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_trycatch_example.htm
Could you confirm it, please?
Alain CabonAlain Cabon

The administrator user deployed the change set, but the (technical) site guest user caused the exception.

What is the email used by this administrator?  

Set Recipients of Email Notifications for Unhandled Apex Exceptions:
Unhandled exception emails are sent by default to the developer specified in the LastModifiedBy field on the failing class or trigger. In addition, you can have emails sent to users of your Salesforce org and to arbitrary email addresses. 
http://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_apex_exception_emails.htm#rn_apex_exception_emails

https://success.salesforce.com/ideaView?id=08730000000BosLAAS
 
Om PrakashOm Prakash
Yes Krisztian, you are right, If DMLException was already handled in code then email should not come. I guessed earlier that your code has only CallOutException mentioned in try-catch.
KrisztianKrisztian
Dear Aladin and Om! Thank for your answers. I would like to specify the question:

How it possible that the SF can send developer script exception email about a DMLException from a try block which catches Expetion? The recipient address is not interest.
Om PrakashOm Prakash
Hi Krisztián, Can you please share the code of “catch block” only which is after line 82.
KrisztianKrisztian
            upsert newContacts A_InId__c; // causes DMLException
            resBody = JSON.SERIALIZE(msg, false);
        }
        catch(Exception ex) {
            system.debug('Exception> ' + ex.getMessage() + ' ' + ex.getStackTraceString());
            Message msg = new Message();
            msg.errorCode = 'UNHANDLED_EXCEPTION';
            msg.message = ex.getMessage() + ' ' + ex.getStackTraceString();
            resBody = JSON.SERIALIZE(msg, false);
        }

        system.debug('response> ' + resBody);
        return resBody;
    }

 
Om PrakashOm Prakash
Thanks for the update Krisztian.
Its really weird.
As per my experience, email should not come because already catch(Exception ex) is there.
I will keep eye on this question and other community members' response as well.
Thanks

 
KrisztianKrisztian
Thanks for your quick reply Om!