Determine if an exception cannot be thrown

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 () .

+4
source share
4 answers

Why exceptions?

Why don't you just let (possibly even both) methods return a bool, indicating success?

It sounds like you had the idea of ​​bools, but for some reason it didn't work. Why the hell not? It is so logical and simple!

Withdrawal handles internal exceptions and returns false, which TransferMoney checks and returns false. It is also very logical that you want these operations to return whether the operation was successful.

 public bool WithdrawMoney() { //Take money from bank account //Exceptions abort the operation and are printed, and return false //Flawless run returns true } public bool TransferMoney() { //Take money from one account and only deposit // on another account if WidthdrawMoney returned true if (WithdrawMoney()) { DepositMoney(); return true; } return false; } 

After all this - I would say that you also need to check that the 2nd WithdrawMoney operation did not work, and in case this happens - rollback the changes that you made with the 1st WithdrawMoney.

+1
source

The method should not behave differently due to any arbitrary condition that is difficult to fulfill and document.

You must reorganize the part of the output method that you want to use internally so that you can use one method from the outside and one from the inside.

 private void MakeWithdrawal() { //Take money from bank account //Exceptions abort the operation } public void WithdrawMoney() { MakeWithdrawal(); //Exceptions are printed } public void TransferMoney() { //Take money from one account and only deposit on another account if no exceptions were caught in WithdrawMoney() MakeWithdrawal(); DepositMoney(); //Exceptions are printed } 
+4
source

Just add a wrapper. Single responsibility applies to methods.

 private void WithdrawMoney() { // Take money from bank account // _Always_ Rethrow } public void WithdrawMoneyPublic() { // call WithdrawMoney and handle the exceptions } 
+3
source

You can add an extra parameter to the function call:

 public void WithdrawMoney(bool throwOnError = false) { //Do stuff catch(Exception ex) { if(throwOnError) throw; } //Do stuff } public void TransferMoney() { WithdrawMoney(true); } 

However, this is taken from the Wireframe Design Guide as an example of code that you should not write. This is a great book.

+2
source

All Articles