Exception Handling

Exception handling in C++ is a mechanism that enables elegant handling of anomalous situations and runtime faults to ensure the program can manage unexpected situations. C++ uses the try, throw and catch keywords to handle exceptions. Code in ‘try’ blocks has the potential to throw exceptions that are raised using `throw` instructions. and ‘catch’ blocks deal with exceptions thrown in the matching ‘try’ block.

Example:

The below example demonstrates the exception handling mechanism using try, throw and catch blocks

C++
// C++ program to demonstrate exception handling

#include <iostream>
#include <stdexcept>
using namespace std;

int main()
{
    try {
        // Declare and initialize two integers.
        int x = 10, y = 0;

        // Check if the divisor is zero.
        if (y == 0)
            // Throw a runtime error exception if divisor is
            // zero.
            throw runtime_error("Divide by zero error");

        // Perform division and print the result (this line
        // won't execute if y is zero).
        cout << "Result: " << x / y << endl;
    }
    catch (const runtime_error& e) {
        // Catch the runtime error exception and print the
        // error message.
        cout << "Exception caught: " << e.what() << endl;
    }

    return 0;
}

Output
Exception caught: Divide by zero error

7 Essential C++ Concepts for Every Developer

C++ is a powerful, high-performance programming language used in a wide range of applications, from game development to systems programming. To master C++, it’s crucial to understand some key concepts that form the foundation of this language. In this article, we will learn seven such essential C++ concepts that every developer should know.

Table of Content

  • 1. Pointers
  • 2. Object-Oriented Programming (OOP) Principles
  • 3. Overloading
  • 4. STL (Standard Template Library)
  • 5. Memory Management
  • 6. Exception Handling
  • 7. Concurrency

Similar Reads

1. Pointers

Pointers are one of the fundamental concepts in C++ which provides the lower-level memory access to the users. Memory addresses are stored in pointers, which are variables. They make direct memory manipulation possible, which is advantageous for operations like hardware access, dynamic memory allocation, and passing arguments by reference....

2. Object-Oriented Programming (OOP) Principles

C++ is an object-oriented programming language that lets us use classes and objects to model real-world entities that helps us to organize the complex programs into manageable and modular code. The following are the main OOP principles in C++:...

3. Overloading

C++ allows function and operator overloading that allows multiple definitions for the same function name or operator that can be differentiated using parameter types and number of parameters. It enhances code readability and reusability....

4. STL (Standard Template Library)

The Standard Template Library(STL) is a powerful feature in C++ that provides a set of general purpose classes and functions for data structures and algorithms. In C++ STL provides the following:...

5. Memory Management

C++ offers manual memory management via new, delete, malloc, and free methods for dynamic memory allocation and deallocation. Proper memory management is crucial to avoid memory leaks and dangling pointers. In C++ stack memory stores information about function calls and local variables. It is handled by the compiler automatically and heap memory must be explicitly handled by the programmer and is utilized for dynamic memory allocation. We also have smart pointer classes in C++({std::shared_ptr}, std::unique_ptr}, std::weak_ptr}) for automatic memory management....

6. Exception Handling

Exception handling in C++ is a mechanism that enables elegant handling of anomalous situations and runtime faults to ensure the program can manage unexpected situations. C++ uses the try, throw and catch keywords to handle exceptions. Code in ‘try’ blocks has the potential to throw exceptions that are raised using `throw` instructions. and ‘catch’ blocks deal with exceptions thrown in the matching ‘try’ block....

7. Concurrency

In C++, the concept of concurrency was first added in C++ 11 to improve the performance of the program and enable multitasking. Concurrency is the ability to carry out several tasks or threads at the same time. Within a process, threads are separate execution sequences that enable code to run in parallel. C++ uses a synchronization primitive called a mutex (mutual exclusion) is used to stop several threads from accessing shared resources at the same time...

Contact Us