Skip to main content
GameDev.net gamedev.net
🔒 Locked

Different between std::exception and std::runtime_error

Started by Darkor Apr 24, 2004 at 7:58 AM 1 replies 400+ views
Original Post
Darkor
Darkor
From the threads I''ve come acrossed while reviweing C++ exceptions, I''ve learned that we should derive from std::exception in order to create our own exception module. This is a little different from the version I found in C++ Gotchas. They derive from the std::runtime_error class. What''s the difference? Also, I''ve been looking for a paper on how to design your own exception framework, does anyone have any links? I did a quick search but came up with naught, what should I look for?
Jingo
Jingo
std::runtime_error is one of many exception classes that are derived from std::exception, it just gives the handler a bit more info about the type of exception that occured. For example, a user may wish to handle runtime errors differently from logic errors, these standard classes give the user the ability to do that.


try
{
...
}
catch(const std::logic_error&)
{
//handle logic errors

}
catch(const std::runtime_error&)
{
//handle runtime errors

}
catch(const std::exception&)
{
//other exceptions

}


runtime_error and logic_error also have exceptions derived from them for more specific errors, such as underflows and overflows, which allows you to handle these errors differently too.

Hope that helps
Oluseyi
Oluseyi
std::runtime_error is a specialization of std::exception, obviously intended to deal with a bunch of runtime errors (indicated in classes derived from it). Obviously, not all your exceptions are going to be "runtime errors", so deriving from std::exception is more semantically correct.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.