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
richardvrichardv 

Weirdness with SObjectException not being caught

See below class.  If you execute go() either anonymously or as test method, the method call to get() will throw an SObjectException even though a try/catch exists for that exact exeption!  Anyone seen this before?  Seems very much like a defect to me.  Please let me know if you feel the same way.

 


global class DemoExceptionIssue {


    global testmethod static void go(){
        final String BAD_FIELD_NAME = 'namexxxxx';
        try{
            (new Account()).get(BAD_FIELD_NAME);
        }catch(SObjectException e){
            System.debug('SObjectException caught as expected (1)');
        }
        //YIKES!  This next line will throw SObjectException
        //  in Spring '10 even though try/catch is in method!
        get(new Account(),BAD_FIELD_NAME);
    }
   
    global static Object get(SObject sobj, String fieldName){
        try{
            return sobj.get(fieldName);
        }catch(SObjectException e){
            System.debug('SObjectException caught as expected (2)');
        }
        return null;
    }
   
}
 


 

Best Answer chosen by Admin (Salesforce Developers) 
richardvrichardv

I figure it out but wow this is a weird one.  See code below.

 


global class DemoExceptionIssue {

 

    global testmethod static void go(){
        final String BAD_FIELD_NAME = 'namexxxxx';
        try{
            (new Account()).get(BAD_FIELD_NAME);
        }catch(SObjectException e){
            System.debug('SObjectException caught as expected (1)');
        }
        get_TheWorkaroundWay(new Account(),BAD_FIELD_NAME);
        //YIKES!  This next line will throw SObjectException
        //  in Spring '10 even though try/catch is in method!
        get_TheWayIWantToWriteButCantSinceItThrowsAnException(new Account(),BAD_FIELD_NAME);
    }
  
    global static Object get_TheWorkaroundWay(SObject sobj, String fieldName){
        Object returnValue = null;
        try{
            returnValue = sobj.get(fieldName);
        }catch(SObjectException e){
            System.debug('SObjectException caught as expected (2)');
        }
        return returnValue;
    }
  
    global static Object get_TheWayIWantToWriteButCantSinceItThrowsAnException(SObject sobj, String fieldName){
        try{
            return sobj.get(fieldName);
        }catch(SObjectException e){
            System.debug('SObjectException caught as expected (2)');
        }
        return null;
    }
}


 

All Answers

richardvrichardv

I figure it out but wow this is a weird one.  See code below.

 


global class DemoExceptionIssue {

 

    global testmethod static void go(){
        final String BAD_FIELD_NAME = 'namexxxxx';
        try{
            (new Account()).get(BAD_FIELD_NAME);
        }catch(SObjectException e){
            System.debug('SObjectException caught as expected (1)');
        }
        get_TheWorkaroundWay(new Account(),BAD_FIELD_NAME);
        //YIKES!  This next line will throw SObjectException
        //  in Spring '10 even though try/catch is in method!
        get_TheWayIWantToWriteButCantSinceItThrowsAnException(new Account(),BAD_FIELD_NAME);
    }
  
    global static Object get_TheWorkaroundWay(SObject sobj, String fieldName){
        Object returnValue = null;
        try{
            returnValue = sobj.get(fieldName);
        }catch(SObjectException e){
            System.debug('SObjectException caught as expected (2)');
        }
        return returnValue;
    }
  
    global static Object get_TheWayIWantToWriteButCantSinceItThrowsAnException(SObject sobj, String fieldName){
        try{
            return sobj.get(fieldName);
        }catch(SObjectException e){
            System.debug('SObjectException caught as expected (2)');
        }
        return null;
    }
}


 

This was selected as the best answer
schulttjschulttj
I've encountered the same problem multiple times, so I don't think it is specific to Spring '10.  You found the same work-around I use - assign the results of the statement that could throw an exception to a variable, then return the variable.  Yes, it very much seems like a defect in apex from the programmer's perspective.