logic_error is a good catchall for βsomething does not make sense hereβ. There may be times when some other type of exception makes more sense, but we will generally just throw a logic_error when throwing exceptions in this book.
When you catch an exception, it includes all of those different types. But it is possible to catch more specific types. It is also possible to catch multiple different types of exception and respond differently to each:
try {
// something that could go bad in different ways
} catch (const out_of_range& e) {
// handle out_of_range exception
} catch (const system_error& e) {
// handle system_error exception
} catch (const exception& e) {
// handle other exceptions
} catch (...) {
// handle all other exceptions
}
When there are multiple catch blocks attached to a single try, they will be checked in order until one matches. Once a catch handles the exception, the rest of the catches are ignored. So the most specific catch should be first, followed by less specific ones. Thus we have exception after the other two more specific exception types.
The final catch in this sample specifies .... That is a way to catch absolutely anything that was thrown. Instead of throwing one of the standard exception types, code can throw its own custom exception types, or some other type of data completely. The ... will catch any of those things.