Search code examples
c++throwinvalid-argument

throw does not show error msg c++


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.



Solution

  • 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.