-2

Final update: Thank you for all the comments and help. I went back to the textbook and copied the whole code to run their program, and realized their throw message never appeared, I assumed there message would appear which is why I was expecting a message to appear on my program which is why I didn't use a try/catch. Thank you again for the help guys/gals.

I'm working on an assignment and for some reason the throw invalid_argument will not display the message "Account balance is too low".

I pretty much took the code from my text book

void setBaseSalary(double salary) {
  if (salary<0.0){
    throw invalid_argument("Salary must be >= 0.0");
   }
}

and created my own....

void Account::setAccountBalance(double accountBalanceVar)
{
    cout << accountBalanceVar << "B4 IF" << endl;
    if (accountBalanceVar < 0.0) {
        cout << accountBalanceVar << "B4 while in IF" << endl; 
        throw invalid_argument("Account balance is too low"); // program err      
        accountBalanceVar = 0.0;
        cout << accountBalanceVar << "In IF" << endl;
    }
    accountBalance = accountBalanceVar;
}

The cout was me debugging where the program stop working, which lead me to the throw.

Here is my full code to replicate the error, i'm using -5 for my input

#include <iostream>
#include <iomanip>
#include "savingAccount.h"
#include "checkingAccount.h"
#include "Account.h"

// header

using namespace std;

int main() {

    double userAccountBalance{ 0.0 };

    cout << "Enter your account Balance: " << endl;
    cin >> userAccountBalance;

    Account myAccount{ userAccountBalance };

    return 0;
}

#include "Account.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <stdexcept>
#include <sstream>

using namespace std;



Account::Account(double accountBalanceVar) {

    setAccountBalance(accountBalanceVar);
    cout << accountBalance << ": Tester: constructor" << endl;
}

double Account::credit(double addBalance)
{
    accountBalance+=addBalance;

    return accountBalance;
}

double Account::debit(double withDrawnVar)
{
    if (accountBalance < withDrawnVar) {

        cout << "Error : You can not withdrawn more than your total balance";
        withDrawnVar = 0; // To ensure unchange

}
    accountBalance -= withDrawnVar;

    return withDrawnVar;
}

Account::~Account()
{
}

void Account::setAccountBalance(double accountBalanceVar)
{
    cout << accountBalanceVar << "B4 IF" << endl;
    if (accountBalanceVar < 0.0) {
        cout << accountBalanceVar << "B4 while in IF" << endl;
        throw invalid_argument("Account balance must be >= 0.0");
        accountBalanceVar = 0.0;
        cout << accountBalanceVar << "In IF" << endl;
    }
    accountBalance = accountBalanceVar;

}

double Account::getAccountBalance()
{
    return accountBalance;
}


#include <iostream>
#include <iomanip>
#include <string>

#ifndef ACCOUNT_H
#define ACCOUNT_H 
class Account
{
public:
    Account(double = 0.0);
    double credit(double);
    double debit(double);


    ~Account();


    void setAccountBalance(double);
    double getAccountBalance();

private:
    double accountBalance{ 0.0 };
    double zeroBalance{ 0.0 };

};

#endif

Update:

What symptoms are you seeing? What inputs are you using? The program stop after the input (Using -5 as input) " cout << accountBalanceVar << "B4 while in IF" << endl; "

The command will display -5, the throw will not display and the program stops.

I did not use a try/catch because the example of the textbook didn't use it, I'm confused why the textbook version works and my doesn't..it's almost identical.


jaye31987
  • 13
  • 5
  • 2
    You'll have to give more details. What do you mean "the throw invalid_argument does not want to work"? What symptoms are you seeing? What inputs are you using? What does the code that calls the function look like? – Michael Albers Mar 25 '17 at 20:46
  • 1
    Where are handling the exception? Where is your `try...catch` statement? – Ervin Szilagyi Mar 25 '17 at 20:50
  • 1
    Are you expecting to see the text printed by the `cout` after the `throw`? The program will never get there. May we have a [mcve] so that we have enough information to provide good advice? – user4581301 Mar 25 '17 at 20:56
  • Who says that exception parameters are always printed? Do you have no catch? – deviantfan Mar 25 '17 at 20:57
  • Waffling over the correct close vote for this question: Unclear what you are asking or off topic (lacking code to reproduce error) – user4581301 Mar 25 '17 at 21:04
  • @jaye31987 Probably, textbook version works, and yours doesn't, because of that _**almost**_ in _almost identical_. – Algirdas Preidžius Mar 25 '17 at 21:11
  • @jaye31987 You should read about exception handling a bit more: http://www.cplusplus.com/doc/tutorial/exceptions/ If your textbook does not speak about `try...catch` than probably you should throw it away. – Ervin Szilagyi Mar 25 '17 at 21:15
  • [Your code works](http://coliru.stacked-crooked.com/a/c84457b863f95927), it probably just doesn't do what you _want_ it to do. The two lines after the `throw` will never be reached, because throwing moves execution to the nearest containing `try` block that can `catch` what you threw. In this case, since the closest containing `try` is in `main()`, throwing causes it to do any required cleanup in `setAccountBalance()`, then back up until it gets back to the `try` block, so it can check if there's a `catch` for `std::invalid_argument`; since it's derived from `std::exception`, the `catch` – Justin Time - Reinstate Monica Mar 25 '17 at 21:20
  • block there works. If there wasn't an appropriate `catch` block there, it would back up some more and try to find the next closest `try` block, and so on until it finds one that can `catch` a `std::invalid_argument`, or terminate execution if it couldn't find one. – Justin Time - Reinstate Monica Mar 25 '17 at 21:22
  • [Here's](http://coliru.stacked-crooked.com/a/4c714eac983c67ca) a little illustration of how `throw` moves execution to the appropriate `catch` block. – Justin Time - Reinstate Monica Mar 25 '17 at 21:55
  • @jaye31987 You're welcome. – Justin Time - Reinstate Monica Mar 25 '17 at 23:59
  • @jaye31987 Also, you don't actually need `type_traits` for anything in there, that's just something I used while testing something, but forgot to remove after. – Justin Time - Reinstate Monica Mar 26 '17 at 01:52

1 Answers1

1

Exceptions are handled by try/catch statements. You would need to add one in order to catch your exception and print its value.

try {
  Account myAccount{ -5.0 }; // Assuming this calls setAccountBalance
} catch (const std::invalid_argument &e) {
  std::cerr << "Exception: " << e.what() << std::endl;
}

If you never catch the exception, the terminate function is called. It will stop your executable. C++ isn't like Java in that unhandled exceptions are not printed. If you're running on Linux and have core file generation enabled then a core file will be generated.

Michael Albers
  • 3,721
  • 3
  • 21
  • 32