捕获多个自定义异常? -C ++ [英] Catch Multiple Custom Exceptions? - C++
问题描述
我是我的第一个C ++编程课程的学生,并且我正在一个项目中,我们必须创建多个自定义异常类,然后在我们的一个事件处理程序中使用 try / catch
块来适当地处理它们。
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 try/catch
block to handle them appropriately.
我的问题是:如何捕获多个自定义异常在我的 try / catch
块中? GetMessage()
是我的异常类中的自定义方法,它以 std :: string
的形式返回异常说明。在下面,我已经包括了项目中的所有相关代码。
My question is: How do I catch my multiple custom exceptions in my try/catch
block? GetMessage()
is a custom method in my exception classes that returns the exception explanation as a std::string
. Below I've included all the relevant code from my project.
感谢您的帮助!
Thanks for your help!
尝试/捕获块
// This is in one of my event handlers, newEnd is a wxTextCtrl
try {
first.ValidateData();
newEndT = first.ComputeEndTime();
*newEnd << newEndT;
}
catch (// don't know what do to here) {
wxMessageBox(_(e.GetMessage()),
_("Something Went Wrong!"),
wxOK | wxICON_INFORMATION, this);;
}
ValidateData()方法
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!");
}
只是我的一个自定义异常类,其他都具有相同的结构
class HourOutOfRangeException
{
public:
// 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() {}
private:
string message;
};
推荐答案
如果您有多个异常类型,并假设有一个异常的层次结构(并且所有异常都从 std :: exception
的某个子类公开派生)从最具体的地方开始,并一直延续到更普遍:
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:
try
{
// 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
}
另一方面,如果您仅对错误消息感兴趣-抛出
相同的例外,用不同的消息说 std :: runtime_error
,然后 catch
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:
try
{
// code throws some subclass of std::exception
}
catch ( const std::exception& e )
{
std::cerr << "ERROR: " << e.what() << std::endl;
}
还记得-按值抛出,按[const]引用捕获。
Also remember - throw by value, catch by [const] reference.
这篇关于捕获多个自定义异常? -C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!