2.5.3 Validating a Vector Index
In the last example, you saw a basic use of the try/catch block to help validate a user input. In this lesson you are going to see how you can throw specific exceptions and include messages to help your catch
block give more information.
The Exception Class
The exception
class is a high level class that handles error types in C++. It is important to understand that the exception
class is the parent of several classes, such as the logic_error
class and the runtime_error
class. We will focus on using different errors from the standard error hierarchy of classes, which includes sub-classes such as the invalid_argument
class.
To catch a specific error, you can specify that you want to catch that error type. For example, you may want to only catch invalid_argument
errors. To catch a broader group of errors, you can specify a parent class, such as logic_error
, runtime_error
, or even just exception
, which will capture any error type that is a subclass to the parent.
The Standard Error Hierarchy
Many of the errors you will see in this course fall under the standard error hierarchy. Some of these errors can be generated automatically, but as you will see, you can also throw your own error. When throwing your own error, you can choose the type however the type should be appropriate.
The following tables show some of the different types of errors available and their typical uses.
logic_error
logic_error
The logic_error
and its sub-classes indicate violations of logical preconditions
invalid_argument
exception class to report invalid arguments
domain_error
exception class to report domain errors
length_error
exception class to report attempts to exceed maximum allowed size
out_of_range
exception class to report arguments outside of expected range
runtime_error
runtime_error
The runtime_error
and its sub-classes indicate conditions only detectable at run time
range_error
exception class to report range errors in internal computations
overflow_error
exception class to report arithmetic overflows
underflow_error
exception class to report arithmetic underflows
Throwing a Custom Error
As mentioned before, C++ doesn’t automatically throw as many errors as other languages. As a developer, this can be useful but you need to be aware of this as invalid conditions can cause undesired effects.
A good example of this is with vectors. When attempting to access a vector value outside the index range, no error is thrown, but no value is returned either. In this example, you are going to see how you can set up to throw your own error as you check the value that users are entering.
To throw a specific error, you use the throw
keyword followed by the type of error you want to throw. Custom errors also require a string description that can be used in your output message.
Here is the basic structure:
To catch these errors, you can either catch the specific type, or use a more generic parent or grandparent type. The parameter type that the exception will take is const&
which represents a constant value, which must be used by convention.
Once you catch the error, you can use the what()
function to retrieve the message that was passed.
Here is a more complete example:
Notice in this example how the custom message is passed to the catch block and printed with the e.what()
call. This example also catches more than just the logic_error by using the generic exception
class.
It is important to note that if you throw an error and do not catch it, your program will terminate. Any error that you set up to throw, make sure you specify how you want to deal with that error in a catch block.
Last updated