C++ / C++ Exception Handling
Working with Standard Exceptions
This tutorial will guide you through the process of working with standard exceptions provided in C++. We'll cover common exceptions and demonstrate how to use them for effective e…
Section overview
5 resourcesCovers exception handling techniques to handle errors gracefully in C++.
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/catchblock 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
- Write a function that throws a
std::length_errorif astd::stringis too long. - Write a program that catches a
std::bad_allocexception 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.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article