Working with Standard Exceptions

Tutorial 3 of 5

Working with Standard Exceptions in C++

Introduction

In this tutorial, we will explore the concept of standard exceptions in C++. We will understand what they are, how they work, and how to handle them for effective error management in our programs.

By the end of this tutorial, you will be able to:
- Understand what standard exceptions are
- Handle standard exceptions in your program
- Use standard exceptions to write safer and more robust code

Prerequisites:
- Basic understanding of C++ programming
- Familiarity with the concept of exceptions

Step-by-Step Guide

Exceptions are unexpected events that occur during the execution of a program. Standard exceptions are predefined exceptions provided by the C++ Standard Library. They are designed to handle common error scenarios, such as dividing by zero, accessing out-of-bounds array elements, etc.

You can handle these exceptions using a try/catch block. The try block contains the code that might throw an exception, and the catch block contains the code to handle the exception.

Here's a simple example:

try {
  // Code that may throw an exception
} catch (const std::exception &e) {
  // Handle exception
  std::cerr << e.what() << '\n';
}

Code Examples

Example 1: Handling std::out_of_range

Here, we'll try to access an element outside the bounds of a std::vector.

#include <iostream>
#include <vector>

int main() {
  try {
    std::vector<int> myVector = {1, 2, 3};
    std::cout << myVector.at(3); // This will throw std::out_of_range
  } catch (const std::out_of_range &e) {
    std::cerr << "Out of Range error: " << e.what() << '\n';
  }
  return 0;
}

The std::vector::at function checks if the requested position is within the valid range. If not, it throws a std::out_of_range exception. In the catch block, we catch this exception and print an error message.

Example 2: Handling std::invalid_argument

In this example, we'll throw a std::invalid_argument exception when a function receives an invalid argument.

#include <iostream>
#include <stdexcept>

void testFunction(int x) {
  if (x <= 0) {
    throw std::invalid_argument("x must be positive");
  }
}

int main() {
  try {
    testFunction(-1); // This will throw std::invalid_argument
  } catch (const std::invalid_argument &e) {
    std::cerr << "Invalid argument: " << e.what() << '\n';
  }
  return 0;
}

In this code, the testFunction checks if the argument is positive. If not, it throws a std::invalid_argument exception. The catch block in the main function catches this exception.

Summary

  • Standard exceptions are predefined exceptions in C++ that handle common error scenarios.
  • Use a try/catch block to handle exceptions.
  • Each standard exception is designed to handle a specific type of error.

After this tutorial, you can explore more about standard exceptions in C++ and how to create custom exceptions.

Additional resources:
- C++ Exception Handling
- C++ Standard Exceptions

Practice Exercises

  1. Write a function that throws a std::length_error if a std::string is too long.
  2. Write a program that catches a std::bad_alloc exception when you try to allocate too much memory.

Solutions

void checkStringLength(const std::string &str) {
  if (str.size() > 100) {
    throw std::length_error("String is too long");
  }
}

int main() {
  try {
    checkStringLength(std::string(500, 'a')); // This will throw std::length_error
  } catch (const std::length_error &e) {
    std::cerr << "Length error: " << e.what() << '\n';
  }
  return 0;
}
int main() {
  try {
    new int[std::numeric_limits<std::size_t>::max()]; // This will throw std::bad_alloc
  } catch (const std::bad_alloc &e) {
    std::cerr << "Memory allocation error: " << e.what() << '\n';
  }
  return 0;
}

In the first exercise, we check the length of a string and throw a std::length_error if it's too long. In the second exercise, we try to allocate a large amount of memory and catch a std::bad_alloc exception.