+ Start a Discussion

How to throw System Defined Exceptions in salesforce?

Hi All,


I have a try catch block in my apex class.




///Webservice call


catch(CallOutException e)




As webservice call will not be made in test methods, i need to throw CalloutException explicitly in test class.


Has anyone tried throwing System defined Exception?





We have wanted to do this, but have not been able to find a way.    I don't believe you can instantiate salesforce defined exceptions - I've always assumed this is because there is some bulit-in exception handling and they don't want your code making this fire.


I'd suggest that your catch block for the system exception throws a new custom exception that can wrap that or a different exception.


This is something that we often hit - you can't engineer some failure scenarios in unit tests, so you have a choice of writing code to pretend a failure occured purely for unit tests or not covering that code.  We often go for the latter option.