Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8500805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:02:14+00:00 2026-06-11T01:02:14+00:00

I have 2 functions: public void WithdrawMoney() { //Take money from bank account //Exceptions

  • 0

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();     
}

What I want is to be able to rethrow an exception that occured in WithdrawMoney(), only if it was called by TransferMoney(). If I just want to withdraw money from an account, the exception must be handled, but does not have to be rethrown as it was not called by another method.

Besides working with bools, there is another solution that comes to my mind. I could look into the stacktrace, see if TransferMoney() called WithdrawMoney() and only rethrow the exception if it did. Or is there a way to see if an exception occured in a method?

I just want to know if you can check whether an exception is throwable in a catch block before throwing it. If I always throw it, the exception will be unhandled when I just call WithdrawMoney() directly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:02:16+00:00Added an answer on June 11, 2026 at 1:02 am

    Why exceptions?

    Why wont you just let (possibly even both) methods return bool, indicating success?

    It sounds like you had the bools idea, but didn’t go for it for some reason. Why the heck not? It’s so logical and simple!

    Withdraw money handles inner exceptions and returns false, which the TransferMoney will check and return false as well. It is also very logical that you will want those 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 that – I would say that you also need to check the 2nd WithdrawMoney operation did not fail, and in case it did – rollback the changes you’ve done with the 1st WithdrawMoney.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two functions which look like this: public static void myFunction1(int a,
I have series of functions with different argument signatures: public void function1 (string s1,
I have the following two functions: public class MyClass { public void Save<TObject>(TObject object)
I have a user control called DashboardUserControl.ascx It has a function: public void setPagination(Boolean
I have a function B that has this declaration: public void B(Class<? extends C>
I have following function in one Activity public void AppExit() { Editor edit =
Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
I have a function that copies binary file public static void copyFile(String Src, String
I have a function defined like this: public static void ShowAbout(Point location, bool stripSystemAssemblies
I have a Java function called testForNull public static void testForNull(Object obj) { if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.