Developing Custom Exception Handler :: C++

Status
Not open for further replies.
Hi.

I would like to create a custom class exception handler. I want to use it for arbitrary exceptions that are not part of the exception library.

I am having a problem with such a class. Here is the code for that exception class.

-----
#ifndef CEXCEPTIONHANDLE_H
#define CEXCEPTIONHANDLE_H

class ExceptionHandle
{
public:
ExceptionHandle(const char *error) : eMessage(error) {}
const char *what() const throw()
{
return eMessage;
}

private:
const char *eMessage;
};

#endif
-----

I basic want to *throw* a character string to the exception handle. Afterward, have a catch function to call what().

Here is an example of a try/catch.

----
try
{
function(x);
}

catch (ExceptionHandle *e)
{
cerr << e->what();
}
-----
function(int X)
{
if (X == 0)
throw ExceptionHandle("Error: X equals zero.");
}
-----

The program I am working on works, but it crashes whenever "throw ExceptionHandle("Error: X equals zero.");" occurs.

Does anyone know what is wrong with my design and/or implementation?

Thanks,
Kuphryn
 
Catching by reference and not by pointer did it for me. Think about it: you're throwing an object, not a pointer (ie. you're not doing throw new Exception..() ). Your exception was just not caught, since the types didn't match. It's like trying to call a function which expects a pointer and passing it an object.
code:<HR>
Code:
catch (ExceptionHandle &e)
{
    cerr << e.what();
}
Oh and use the code tag please! ;)
 
Status
Not open for further replies.