Why does the caller method take care of what callee does? Will another action be taken if there is a problem sending the file?
try { callee(..); } catch (SomeException e) { if (e.Message == "Sending file failed") {
Exceptions should generally be exceptional. Your typical logical flow should not require to be thrown and broken. When they are thrown, this usually happens due to an error in your code *. Therefore, the main purpose of the exception:
- to stop the flow of logic before the error does more damage, and
- to provide information about the state of the system when an error occurs, so you can determine the source of the error.
Given this, you should generally only use exceptions if:
- you plan to wrap the exception with additional data (as in your example) and throw a new exception, or
- You know that there will be no further negative consequences from continuing your logical flow: you realize that the thing you tried to do is unsuccessful, and no matter how it failed, you are at peace with this. In these cases, you should always take the opportunity to register errors.
If there is a fairly common case of failure (for example, the user provided incorrect credentials), this should not be considered an exception. Rather, you should structure your callee method signature so that its return result provides all the details that caller should know about what went wrong.
* A notable exception is that something really unexpected happens, for example, if someone disconnected the network cable from the computer in the middle of a transaction.
source share