Lokang 

C++ and MySQL

Standard Exception Classes

C++ provides a hierarchy of standard exception classes in the C++ Standard Library, designed to represent different types of errors that might occur during program execution. These classes are derived from the base class std::exception and are found in the <exception> and <stdexcept> headers.

Here’s an overview of the most commonly used standard exception classes:

1. std::exception

  • Header: <exception>
  • Description: This is the base class for all standard exceptions in C++. It defines the what() method, which returns a C-style character string describing the exception.
  • Usage: You can catch any exception derived from std::exception using this base class.

Example:

try {
   throw std::exception();
} catch (const std::exception& e) {
   std::cerr << "Caught: " << e.what() << std::endl;
}

2. std::runtime_error

  • Header: <stdexcept>
  • Description: This class represents errors that occur during the program's execution. It is a subclass of std::exception.
  • Common Use Case: Used for general runtime errors like logical errors or problems that can't be detected before runtime.

Example:

try {
   throw std::runtime_error("Runtime error occurred!");
} catch (const std::runtime_error& e) {
   std::cerr << "Caught: " << e.what() << std::endl;
}

3. std::logic_error

  • Header: <stdexcept>
  • Description: This class represents errors that can be detected by the program logic, typically errors that arise from faulty logic within the code.
  • Common Use Case: Used for errors that are the result of logical flaws in the program, like invalid arguments or out-of-range errors.

Example:

try {
   throw std::logic_error("Logic error occurred!");
} catch (const std::logic_error& e) {
   std::cerr << "Caught: " << e.what() << std::endl;
}

4. std::out_of_range

  • Header: <stdexcept>
  • Description: This class is a subclass of std::logic_error and represents errors where a value is outside the valid range, such as accessing elements outside the bounds of a container.
  • Common Use Case: Thrown by functions like std::vector::at() when an attempt is made to access an element outside its valid range.

Example:

#include<vector>#include <stdexcept>try {
   std::vector<int> v(5);
   v.at(10) = 100;  // Out of range access
} catch (const std::out_of_range& e) {
   std::cerr << "Caught: " << e.what() << std::endl;
}

5. std::invalid_argument

  • Header: <stdexcept>
  • Description: This class is another subclass of std::logic_error and is used to represent errors that involve invalid arguments being passed to a function.
  • Common Use Case: Thrown when an argument to a function is not valid or does not satisfy certain preconditions.

Example:

#include<stdexcept>void checkValue(int value) {
   if (value <= 0) {
       throw std::invalid_argument("Value must be positive.");
   }
}
try {
   checkValue(-1);
} catch (const std::invalid_argument& e) {
   std::cerr << "Caught: " << e.what() << std::endl;
}

6. std::length_error

  • Header: <stdexcept>
  • Description: This class is a subclass of std::logic_error and represents errors where an operation is attempted that exceeds the maximum allowed size.
  • Common Use Case: Used when an operation exceeds the maximum size of a data structure, such as trying to create a string longer than the allowed limit.

Example:

#include<stdexcept>try {
   throw std::length_error("Exceeded maximum length.");
} catch (const std::length_error& e) {
   std::cerr << "Caught: " << e.what() << std::endl;
}

7. std::bad_alloc

  • Header: <new>
  • Description: This class is derived directly from std::exception and is used to indicate failure in dynamic memory allocation, typically when new fails to allocate memory.
  • Common Use Case: Thrown by the new operator when memory allocation fails.

Example:

#include<new>#include <iostream>try {
   int* arr = newint[100000000000];  // Attempt to allocate a huge array
} catch (const std::bad_alloc& e) {
   std::cerr << "Caught: " << e.what() << std::endl;
}

Summary of Exception Classes Hierarchy

Here’s a brief hierarchy of the most common standard exception classes:

  • std::exception
    • std::logic_error
      • std::invalid_argument
      • std::domain_error
      • std::length_error
      • std::out_of_range
    • std::runtime_error
      • std::range_error
      • std::overflow_error
      • std::underflow_error
    • std::bad_alloc

These standard exception classes provide a structured way to handle different kinds of errors in your C++ programs. By understanding and using these classes, you can write more robust and maintainable code