2017-03-25 3 views
-2

Endgültiges Update: Vielen Dank für alle Kommentare und Hilfe. Ich ging zurück zum Lehrbuch und kopierte den ganzen Code, um ihr Programm auszuführen, und realisierte, dass ihre Wurfnachricht nie erschien, ich nahm an, dass dort eine Nachricht erscheinen würde, weshalb ich eine Nachricht auf meinem Programm erwartete, weshalb ich es nicht tat Verwenden Sie einen Versuch/Fang. Danke nochmal für die Hilfe Jungs/Mädels.throw zeigt keinen Fehler msg C++

Ich arbeite an einer Zuweisung und aus irgendeinem Grund die throw invalid_argument wird nicht angezeigt, die Meldung "Kontostand ist zu niedrig".

Ich habe so ziemlich den Code aus meinem Textbuch

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

und meine eigene ....

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

Die cout wurde mir das Debuggen, wo das Programm aufhören zu arbeiten, die mich führen zum Wurf.

Hier ist meine vollständige Code, den Fehler zu wiederholen, ich bin mit -5 für meine Eingabe

#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:

Welche Symptome sehen Sie? Welche Eingaben verwendest du? Der Programmstopp nach dem Eingang (-5 Verwendung als Eingabe) "cout < < accountBalanceVar < < "B4, während in IF" < < endl;"

Der Befehl wird -5 anzuzeigen, wird nicht der throw anzuzeigen und Das Programm stoppt.

Ich habe keine try/catch verwendet, weil das Beispiel des Lehrbuchs es nicht verwendet hat, ich bin verwirrt, warum die Lehrbuchversion funktioniert und mein tut es nicht .. es ist fast identisch.


+2

Sie müssen mehr Details angeben. Was meinst du mit "der throw invalid_argument will nicht arbeiten"? Welche Symptome siehst du? Welche Eingaben verwendest du? Wie sieht der Code aus, der die Funktion aufruft? –

+1

Wo wird die Ausnahme behandelt? Wo ist deine "Versuch ... Fang" Aussage? –

+1

Erwarten Sie, den Text zu sehen, der durch den 'cout' nach dem' throw' gedruckt wird? Das Programm wird nie dorthin gelangen. Können wir eine [mcve] haben, damit wir genug Informationen haben, um einen guten Rat zu geben? – user4581301

Antwort

0

Ausnahmen werden von try/catch-Anweisungen behandelt. Sie müssen eine hinzufügen, um Ihre Ausnahme zu erfassen und ihren Wert zu drucken.

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

Wenn Sie die Ausnahme nie fangen, die terminate Funktion aufgerufen wird. Es wird Ihre ausführbare Datei stoppen. C++ ist nicht wie Java, da nicht behandelte Ausnahmen nicht gedruckt werden. Wenn Sie unter Linux arbeiten und die Kerndateigenerierung aktiviert ist, wird eine Core-Datei generiert.