0

I have a problem while throwing/catching custom exceptions.

Context: I am making a property (as in real estate) manager that stores properties, tenants, bank accounts, and transactions as classes, and combines them under a contract.

Each transaction will be a node under a doubly linked list.

I was feeding the transaction constructor a nullptr as in_tnumber to see what it will do.

Here is the transaction class:

// manager is a namespace that is used for the entire program
// Each transaction will be stored as a node on a doubly linked list
class transaction
{
public:
    transaction(const char *in_tnumber,
                const int &in_amount,
                manager::transaction *in_head) 
    throw(std::bad_alloc, manager::pexception);

    ~transaction()

    // Other functions...
private:
    // Links for the list.
    manager::transaction *head;
    manager::transaction *tail;

    // Transaction number
    char *tnumber;

    // Transaction amount
    int tamount;

    char *remarks
};

transaction::transaction(const char *in_tnumber,
                         const int &in_amount,
                         manager::transaction *in_head)
throw(std::bad_alloc, manager::pexception)
{
    if ((in_tnumber == nullptr) || (in_tnumber[0] == '\0'))
        throw manager::pexception("Transaction number cannot be empty ");

    if (in_tamount <= 0)
        throw manager::pexception("Transaction amount cannot be empty ");

    tnumber = new char[strlen(in_tnumber) + 1];
    strcpy(tnumber, in_tnumber);

    tamount = in_tamount;

    remarks = nullptr;

    tail = nullptr;
}

transaction::~transaction()
{
    delete [] tnumber;
    delete [] remarks;
}

I also have an exception class that is as such

class pexception
{
public:
    pexception(char *in_message);
    ~pexception() { delete [] message; }

    char* get_message() const { return message; }
private:
    char *message;
};

pexception::pexception(const char *in_message)
{
    message = new char[strlen(in_message) + 1];
    strcpy(message, in_message);
}

When an exception is thrown by the transaction constructor, I get this:

Transaction number cannot be empty

EXITING...

Error in /storage/dimos/projects/build-soft_dev_sat_Mk_III-Debug/soft_dev_sat_Mk_III': double free or corruption (fasttop): 0x00000000006f60c0

and Qt Creator says that the program has crashed.

main() looks like this

int main()
{
    try {
    transaction test("", // As opposed to "1234567890" or something like that
                     300,
                     nullptr);

    cout << "Tnumber: " << test.get_tnumber();
    }
    catch(manager::pexception e)
    {
        cout << e.get_message();
        cout << "\nEXITING...\n";
        return -1;
    }

    return 0;
}

I am on command line (debian) How do I fix this? It works fine when in_tnumber is filled in and no exceptions are thrown.

Thanks for any answers. Sorry for the long code.

Dimos
  • 33
  • 10
  • 2
    You've violated the [rule of three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) in your exception class. You should really just use `std::string` instead of raw `char*`s – Miles Budnek Jul 08 '17 at 11:43
  • Thanks a lot for the help mate. I'll recompile tomorrow morning. – Dimos Jul 08 '17 at 13:32

0 Answers0