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
Ankita Gupta 65Ankita Gupta 65 

exception handling Issue

I'm a little new to exception handling. I've created a very basic scenario where I want to handle an exception in trigger.
Picklist values in field TestPicklist__c of object TestException__c: Yes, No
I'm updating this field with other value than Yes/No so that exception is throw.
trigger TestExceptionHandling on TestException__c (before insert) {
if(trigger.isBefore && trigger.isInsert)
{
    try{
    for(TestException__c testObj:trigger.New)
    {
          
        testObj.TestPicklist__c='Undecided';
        
        
    }
    }
    catch(Exception e)
        {
            System.debug('Error is here'+e.getMessage());
           
        }
}
}

But this code does not give anything in debug logs. Plus the message on page is Salesforce specific message for an error. I want to see the error in debug logs as well.
'Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger TestExceptionHandling caused an unexpected exception, contact your administrator: TestExceptionHandling: data changed by trigger for field TestPicklist: bad value for restricted picklist field: Undecided'
Rohit B ☁Rohit B ☁
Hi Ankita,
you can five any value to picklist and it will save that value irrespective of values available for picklist. You can say it is a advantage/disadvantage in SFDC for picklist. Try with any numeric data type with basic devide by zero error.

Hope it helps :)
 
Ankita Gupta 65Ankita Gupta 65
Hi @Rohit, This does give me error but probably not going inside the catch I've written.