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