Alex Alex - 1 month ago 5
C++ Question

Catch Multiple Custom Exceptions? - C++

I'm a student in my first C++ programming class, and I'm working on a project where we have to create multiple custom exception classes, and then in one of our event handlers, use a

block to handle them appropriately.

My question is: How do I catch my multiple custom exceptions in my
is a custom method in my exception classes that returns the exception explanation as a
. Below I've included all the relevant code from my project.

Thanks for your help!

try/catch block

// This is in one of my event handlers, newEnd is a wxTextCtrl
try {
newEndT = first.ComputeEndTime();
*newEnd << newEndT;
catch (// don't know what do to here) {
_("Something Went Wrong!"),
wxOK | wxICON_INFORMATION, this);;

ValidateData() Method

void Time::ValidateData()
int startHours, startMins, endHours, endMins;

startHours = startTime / MINUTES_TO_HOURS;
startMins = startTime % MINUTES_TO_HOURS;
endHours = endTime / MINUTES_TO_HOURS;
endMins = endTime % MINUTES_TO_HOURS;

if (!(startHours <= HOURS_MAX && startHours >= HOURS_MIN))
throw new HourOutOfRangeException("Beginning Time Hour Out of Range!");
if (!(endHours <= HOURS_MAX && endHours >= HOURS_MIN))
throw new HourOutOfRangeException("Ending Time Hour Out of Range!");
if (!(startMins <= MINUTE_MAX && startMins >= MINUTE_MIN))
throw new MinuteOutOfRangeException("Starting Time Minute Out of Range!");
if (!(endMins <= MINUTE_MAX && endMins >= MINUTE_MIN))
throw new MinuteOutOfRangeException("Ending Time Minute Out of Range!");
if(!(timeDifference <= P_MAX && timeDifference >= P_MIN))
throw new PercentageOutOfRangeException("Percentage Change Out of Range!");
if (!(startTime < endTime))
throw new StartEndException("Start Time Cannot Be Less Than End Time!");

Just one of my custom exception classes, the others have the same structure as this one

class HourOutOfRangeException
// param constructor
// initializes message to passed paramater
// preconditions - param will be a string
// postconditions - message will be initialized
// params a string
// no return type
HourOutOfRangeException(string pMessage) : message(pMessage) {}
// GetMessage is getter for var message
// params none
// preconditions - none
// postconditions - none
// returns string
string GetMessage() { return message; }
// destructor
~HourOutOfRangeException() {}
string message;


If you have multiple exception types, and assuming there's a hierarchy of exceptions (and all derived publicly from some subclass of std::exception,) start from the most specific and continue to more general:

    // throws something
catch ( const MostSpecificException& e )
    // handle custom exception
catch ( const LessSpecificException& e )
    // handle custom exception
catch ( const std::exception& e )
    // standard exceptions
catch ( ... )
    // everything else

On the other hand, if you are interested in just the error message - throw same exception, say std::runtime_error with different messages, and then catch that:

    // code throws some subclass of std::exception
catch ( const std::exception& e )
    std::cerr << "ERROR: " << e.what() << std::endl;

Also remember - throw by value, catch by [const] reference.