Exceptions in a function

Hi, So i have this program that is basically an ATM program that prompts the user to either: deposit money, withdraw money or quit the program. Then you just plug in what you want.
What I'm having problems with is that I'm suppose to rewrite the code in the run() function of the ATM class so that it handles the overdraft causing the WithdrawException. In this case, the ATM should check and see if the savings account has enough money to cover the difference. If the savings account has enough money:
- Withdraw the difference from the savings account. Make sure you remember to empty out the balance of the checking account.
- Otherwise, print an error message that there is insufficient funds in the savings account and re-prompt the user for another command.

Account.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "Account.h"
#include "DepositException.h"
#include "WithdrawException.h"
#include <iostream>
#include <cstdlib>
#include <stdexcept>

void Account::deposit(double amount) {
	try {
		if (amount > 0)
			balance += amount;

		while (amount < 0) {
			throw DepositException();
		}

	} catch (DepositException) {
		std::cout << "Negative Deposit amount not allowed" << std::endl;
	}

}

void Account::withdraw(double amount) {
try {
	if ((amount > balance) || (amount < 0))
		throw WithdrawException ();

	else
	balance -= amount;
}


catch (WithdrawException) {
std::cout << "Insufficient Funds" << std::endl;
}
}


ATM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include "ATM.h"

ATM::ATM() {
	checking = new Account();
	savings = new Account(100);
}

ATM::~ATM() {
	delete checking;
	delete savings;
}

void ATM::run() {
	while(1) {
		char cmd='\0';
		double amount = 0.0;
		std::cout.setf(std::ios::fixed);
		std::cout.setf(std::ios::showpoint);
		std::cout << "Would you like to deposit money (d), withdraw money (w) or quit (q)?" << std::endl;
		std::cin >> cmd;
		if( cmd == 'd' ) {
			std::cout << "Enter the amount you want to deposit" << std::endl;
			std::cin >> amount;
			checking->deposit(amount);
			std::cout << "Current checking balance: " << std::setprecision(2) << checking->getBalance() << std::endl;
		} else if (cmd == 'w') {
			std::cout << "Enter the amount you want to withdraw" << std::endl;
			std::cin >> amount;
			try {
			checking->withdraw(amount);
			throw Overdraft (amount);
			}
			std::cout << "Current checking balance: " << std::setprecision(2) <<  checking->getBalance() << std::endl;
		} else if (cmd == 'q' ) {
			std::cout << "Ending ATM session" << std::endl;
			break;
		} else {
			std::cout << "Unrecognized command" << std::endl;
		}
	}
}

catch (Overdraft e) {
	if ( (checking(balance) + savings(balance)) > e)
		e = e - checking
	else {
		savings -> withdraw(e)
	}
}

int main() {
	ATM atm;
	atm.run();
}
Any help would be appreciated!
catch-statements should terminate a try-block inside a function or method implementation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void example()
{
    int a; // Visible in catch-block

    try
    {
        int b; // Unvisible in catch-block

        for (unsigned int i = 10; i > 0; --i)
        {
            int *p = new int[1000]; // Unvisible in catch-block
        }
    }
    catch (bad_alloc)
    {
        int c; // Visible in catch-block

        clog << "out of memory" << endl;
    }

}


Only exception thrown anywhere inside the immediately preceding try-block will be caught by a catch-block.

The same rules for symbol visibility as in any other block also apply to catch blocks.
Last edited on
catch-statements should terminate a try-block inside a function or method implementation.

@ tcs: This one is debatable since it's common and allowed for an object to throw an exception from within it's constructor and let the calling scope choose whether to catch and handle it or not.

@ OP: When tcs says this:
Only exception thrown anywhere inside the immediately preceding try-block will be caught by a catch-block.

He forgot to mention that if the catch block does not handle the specific type of exception thrown that it will pass it on to the next scope above it. Look at this code if that doesn't make sense yet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Example program
#include <iostream>

void func_B()
{
    try
    {
        throw int(3);
    }
    catch(double Q)
    {
        std::cout << "This message is from func_B: " << Q;
    }
    
    std::cout << "This will not execute\n";
}

void func_A()
{
    try
    {
        func_B();
    }
    catch(int& A)
    {
        std::cout << "This message is from encapsulating scope of 'func_A': " << A;
    }
}

int main()
{
  func_A();
 std::cout << "\nException caught and handled. Program still terminates clean.";
}

Here we see that 'func_A()' calls 'func_B()' and the later throws an exception unconditionally. Even though there is a catch block inside of "func_B()', that is not the catch block that is used since it only handles double types that get thrown. But instead of terminating the program, as would happen with any uncaught exception, the program flow passes on to 'func_A()' which catches and handles the exception.

Your problem is that your catch isn't associated with any try block, which is what tcs is getting at. But I thought that the point that I posted about was too important to omit.
Topic archived. No new replies allowed.