function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SidharthSidharth 

Parse 'exception code' in Apex

Is there a way to parse just the exception code while making calls like insert user, System.setPassword etc.

The error message show: 
Insert failed. First exception on row 0; first error: PORTAL_USER_ALREADY_EXISTS_FOR_CONTACT, portal user already exists for contact: []
Failure : INVALID_NEW_PASSWORD: Your password must be at least 5 characters long.

But now sure how to just get PORTAL_USER_ALREADY_EXISTS_FOR_CONTACT , INVALID_NEW_PASSWORD

List of exception codes: https://www.salesforce.com/developer/docs/api/Content/sforce_api_calls_concepts_core_data_objects.htm#i1421192
Best Answer chosen by Sidharth
SidharthSidharth
        System.debug(e);
        System.debug(e.getMessage());
        System.debug(e.getCause());
        System.debug(e.getLineNumber());
        System.debug(e.getMessage());
        System.debug(e.getStackTraceString());
        System.debug(e.getTypeName());
        if(e.getTypeName() == 'System.DmlException'){
            System.debug(e.getDmlFieldNames(0));
            System.debug(e.getDmlFields(0));
            System.debug(e.getDmlId(0));
            System.debug(e.getDmlIndex(0));
            System.debug(e.getDmlMessage(0));        
            System.debug(e.getDmlStatusCode(0));    
            System.debug(e.getDmlType(0));        
            System.debug(e.getNumDml());
        }