📖
C++
  • 1. C++ Basics
    • 1.1 Input, Output, and Program Structure
      • 1.1.1 Welcome to Data Structures in C++
      • 1.1.2 Hello World
      • 1.1.3 Input and Output
      • 1.1.4 getline and cin
      • 1.1.5 Program Structure
    • 1.2 Basic Data Types
      • 1.2.1 Basic Data Types
        • 1.2.1.1 Differences between C++ and Java Data Types and Variables
      • 1.2.2 Strings and Characters
      • 1.2.3 Numbers
      • 1.2.4 Booleans
    • 1.3 Conditional Statements
      • 1.3.1 Conditional Statements
      • 1.3.2 Basic If/Else Statements
      • 1.3.3 Comparing Strings
      • 1.3.4 Logical Operators
    • 1.4 Loops
      • 1.4.1 Loops
      • 1.4.2 For Loops
      • 1.4.3 While Loops
      • 1.4.4 Searching a String
    • 1.5 Functions in C++
      • 1.5.1 Functions in C++
      • 1.5.2 Defining and Calling Functions
      • 1.5.3 Passing by Reference vs Value
      • 1.5.4 Function Prototypes
  • 2. Going Beyond the Basics
    • 2.1 Vector Basics
      • 2.1.1 Vector Basics
      • 2.1.2 Creating and Accessing Vectors
      • 2.1.3 Inserting into a Vector
      • 2.1.4 Looping Through a Vector
    • 2.2 Function Default Values
      • 2.2.1 Function Default Values
      • 2.2.2 Default Values
      • 2.2.3 Default Values with a Prototype
      • 2.2.4 Example: Splitting a String
    • 2.3 Structs
      • 2.3.1 Structs
      • 2.3.2 Defining and Accessing Structs
      • 2.3.3 Using Structs: Line Length
    • 2.4 File Input/Output
      • 2.4.1 File Input/Output
      • 2.4.2 Reading in a File
      • 2.4.3 Processing a File
      • 2.4.4 Writing to a File
      • 2.4.5 Creating an Input Stream from a String
    • 2.5 Error Handling
      • 2.5.1 Error Handling
      • 2.5.2 Validating a Number
      • 2.5.3 Validating a Vector Index
      • 2.5.4 Throwing Other Values
  • 3. Libraries
    • 3.1 Header Files
      • 3.1.1 Header Files
      • 3.1.2 Header File
      • 3.1.3 Header and Implementation File
      • 3.1.4 Safer Header
    • 3.2 Using Libraries
      • 3.2.1 Using a Utilities Library
      • 3.2.2 The Util Library
  • 4. 2D Vectors, Stacks, and Queues
    • 4.1 2D Vectors
      • 4.1.1 2D Vectors
      • 4.1.2 The 2D Vector
      • 4.1.3 Creating a 2D Vector
    • 4.3 Stacks
      • 4.3.1 Stacks
      • 4.3.2 Basic Stack
      • 4.3.3 Stack Example: Reverse a String
    • 4.5 Queues
      • 4.5.1 Queues
      • 4.5.2 Basic Queues
      • 4.5.3 Queue Example: Next in Line
  • 5. Sets and Maps
    • 5.1 Pairs and Iterators
      • 5.1.1 Pairs and Iterators
      • 5.1.2 Pairs
      • 5.1.3 Iterators
    • 5.3 Sets
      • 5.3.1 Sets
      • 5.3.2 Basic Sets
      • 5.3.3 Iterating Through a Set
      • 5.3.4 Sets of Struct Values
    • 5.4 Maps
      • 5.4.1 Maps
      • 5.4.2 Map Basics
      • 5.4.3 Iterating Through a Map
      • 5.4.4 Updating Maps
  • 6. Recursion
    • 6.1 Functional Recursion
      • 6.1.1 Functional Recursion
      • 6.1.2 Basic Recursive Problem: Exponential
      • 6.1.3 Recursion Example: Reverse String
      • 6.1.4 Recursion Example: Make Sum
    • 6.2 Procedural Recursion
      • 6.2.1 Procedural Recursion
      • 6.2.2 Print Binary
      • 6.2.3 Print Permutations
      • 6.2.4 Depth vs Breadth Search
  • 7. Pointers, Linked Lists, and Graphs
    • 7.1 Pointers
      • 7.1.1 Pointers
      • 7.1.2 Assigning and Updating Pointers
      • 7.1.3 Pointers and Functions
      • 7.1.4 Pointers and Data Structures
    • 7.2 Linked Lists
      • 7.2.1 Linked Lists
      • 7.2.2 Basic Linked List
      • 7.2.3 Linked List and Recursion
      • 7.2.4 Example: Sorted Phone Book
      • 7.2.5 Doubly Linked List
    • 7.3 Graphs
      • 7.3.1 Graphs
      • 7.3.2 Basic Example: Breadth First Search
      • 7.3.3 Application: Connecting Cities
Powered by GitBook
On this page
  • The Exception Class
  • The Standard Error Hierarchy
  • Throwing a Custom Error
  • Try This Example
  1. 2. Going Beyond the Basics
  2. 2.5 Error Handling

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

The logic_error and its sub-classes indicate violations of logical preconditions

Sub-class
Example

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

The runtime_error and its sub-classes indicate conditions only detectable at run time

Sub-class
Example

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:

throw error_type("Message here");

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:

int num;
try {
    cin >> num;
    if (num != 5) {
        throw logic_error("Not my favorite number");
    }
}
catch (exception const& e) {
    cout << e.what() << endl;
}

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.

Previous2.5.2 Validating a NumberNext2.5.4 Throwing Other Values

Last updated 3 years ago

Try This Example