I have 2 functions:
public void WithdrawMoney() { //Take money from bank account //Exceptions abort the operation and are printed //Rethrow exception if called by TransferMoney() } public void TransferMoney() { //Take money from one account and only deposit on another account if no exceptions were caught in WithdrawMoney() WithdrawMoney(); DepositMoney(); }
I want to be able to recover the exception that occurred in WithdrawMoney () only if it was called TransferMoney () . If I just want to withdraw money from my account, the exception should be handled, but it should not be returned, since it was not called by another method.
Besides working with bools, there is another solution that comes to my mind. I could look in stacktrace, see if TransferMoney () is called WithdrawMoney () , and just rename the exception if that happens. Or is there a way to find out if an exception occurred in a method?
I just want to know if it is possible to check whether the exception will be thrown in the catch block before throwing it. If I always throw it, the exception will be unhandled if I just call WithdrawMoney () .
source share