You need to sign in to do that
Don't have an account?
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;
}
}
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
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;
}
}