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
Ron WildRon Wild 

Exceptions slipping through try-catch?

I've discovered that the following code will catch an invalid decimal exception (e.g. value = ' ') ...

 

 

 

global static Decimal toDecimalIfValid(String value) { Decimal result; if (value <> null) { try { result = Decimal.valueOf(value); } catch (Exception ex) { System.debug('Error converting '+value+' to decimal.'); } } return result; }

 

 

 

... but this code won't:

 

 

global static Decimal toDecimalIfValid(String value) { if (value <> null) { try { return Decimal.valueOf(value); } catch (Exception ex) { System.debug('Error converting '+value+' to decimal.'); } } return null; }

 

 

 

 

Both use try-catch to trap the error.  Is this a bug?  A feature?

 

- Ron

 

Hargobind_SinghHargobind_Singh

Great observation !! 

 

Yes, you are right, if the return statement is producing an exception, catch clause is not called at all.


Something for Salesforce team to think about :)

 

..

 

HS 

Keith654Keith654
Having just wasted an hour before finding this post I suggest this "feature" needs emphasising in the documentation on exceptions. Its a bad implementation call that presumably can't be fixed now for backward compatibility reasons.