Running test for the exercise

Where i run tests for the exercise, it states: AN ERROR OCCURRED

We received the following error when we ran your code:

but then it dose not say anything else, what the error is or something. It is blank

Can you make a small change to the code and run tests again to see if it works the second time?

Could you post your code here as text in a code block (inside ```) so people can copy paste to debug?

In addition to what IsaacG said: When an exception gets thrown where the test suite doesn’t expect one you would get an empty error message. I cannot say if that’s the case here but it has been in similar solutions in the past.

hy thank you for the replay. Yes i already try that. Here is my code

header file:

#ifndef BANKACCOUNT_BANK_H
#define BANKACCOUNT_BANK_H

#include
#include
#include
#include
#include

namespace Bankaccount{

class Bankaccount{
    public:
    Bankaccount();
    virtual ~Bankaccount() = default;

    //methods
    void open();
    void close();
    [[nodiscard]] int balance() const;
    void deposit(int amount);
    void withdraw(int amount);

    //variables
    private:
        int m_balance;
        bool m_isOpen;
        std::mutex m_mutex;

};

}

#endif //BANKACCOUNT_BANK_H

cpp file:

#include"bank.h"

namespace Bankaccount{

Bankaccount::Bankaccount():m_balance{0},m_isOpen{false}
{}

void Bankaccount::open() {
    if(m_isOpen){
        throw std::domain_error("Account open !");
    }
    std::unique_lock<std::mutex> lock(m_mutex);
    m_isOpen = true;
}

void Bankaccount::close() {
    if(!m_isOpen){
        throw std::domain_error("Account closed !");
    }
    std::unique_lock<std::mutex> lock(m_mutex);
    m_isOpen = false;
    m_balance = 0;
}

int Bankaccount::balance() const{
    if(!m_isOpen){
        throw std::domain_error("Account closed !");
    }
    return m_balance;
}

void Bankaccount::deposit(int amount) {
    if(!m_isOpen || amount < 0){
        throw std::domain_error("Can't do it !\n");
    }
    std::unique_lock<std::mutex> lock(m_mutex);
    m_balance += amount;
}

void Bankaccount::withdraw(int amount) {
    if(!m_isOpen || amount > m_balance || amount < 0){
        throw std::domain_error("Can't do it !\n");
    }
    std::unique_lock<std::mutex> lock(m_mutex);
    m_balance -= amount;
}

}

1 Like

I noticed that the .cpp file includes "bank.h".
Could you please double-check that?
The two files you are supposed to modify are bank_account.h and bank_account.cpp

I think I know what’s the problem: This solution throws std::domain_error.
But the tests expect a std::runtime_error.
When they get an unexpected exception it gets reported in a way that the test runner cannot interpret, so it shows an empty error message.

If you replace each std::domain_error with std::runtime_error the tests should pass.

BTW: I recommend requesting mentoring. There are few things in this solution that might be worth pointing out or talking about.

1 Like

hy thank you, yes if i replace std::domain_error with std::runtime_error, than all tests pass. :slight_smile:

tnx i will do tha next time :slight_smile: