I created a static method to send exception email to developer name exptonEmail(). I have two classes name Class A and Class B. I am using the exptonEmail method in both class's method in catch block. If both class methods are called in same execution context and if there is a exception occurred for both class's method then how the exptonEmail will be send or how many times the email will be sent. What will be the best Salesforce practice to reuse exptonEmail in different methods catch block.
The answer depends on what happens in the transaction where the call to
If the transaction ends in an uncaught exception, it doesn't matter how many times
expTonEmailis called as no emails will be sent for the transaction.
If the calls to
expTonEmailare within a savepoint-rollback block, the emails will not be sent.
If you are running in a sandbox where email deliverability is not All, the emails won't be sent.
Otherwise, you will get outgoing emails for every call to the method (provided you don't exceed Apex outbound email limits)
It's good practice to have exception handling at a high level of your software architecture. There are situations where catching exceptions at a lower level is acceptable but I would not recommend it. Let the top level classes decide what to do with exceptions. If you have exceptions at a lower level for logging (which is acceptable) rethrow the exception. Catching exceptions at low levels in the code and sweeping the error under the rug should is the real exception. In your case having exceptions propagate to the top will allow you more flexibility in how many times you call that email method.