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
SurekaSureka 

How to throw System Defined Exceptions in salesforce?

Hi All,

 

I have a try catch block in my apex class.

 

try

{

///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?

 

Thanks

Sureka

bob_buzzardbob_buzzard

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.