How use try catch in C plus plus - GitMasterNikanjam/C_WiKi GitHub Wiki

In C++, try and catch are keywords used for exception handling. Exception handling is a mechanism to handle runtime errors or exceptional situations that occur during the execution of a program.

Here's how try and catch work:

1- try block: The try block is used to enclose the code that might throw an exception. If an exception occurs within this block, the program looks for a matching catch block to handle the exception.

2- catch block: A catch block is used to catch and handle exceptions thrown within the corresponding try block. It follows the try block and contains code that specifies what action to take when a particular type of exception occurs.

Here's a basic syntax for using try and catch:

try {
    // Code that might throw an exception
} catch (ExceptionType1 e1) {
    // Handler for ExceptionType1
} catch (ExceptionType2 e2) {
    // Handler for ExceptionType2
} catch (...) {
    // Generic catch block for any other exceptions
}
  • The try block contains the code that may throw an exception.
  • Each catch block catches a specific type of exception. You can have multiple catch blocks to handle different types of exceptions.
  • The ellipsis ... in the last catch block serves as a catch-all for any exceptions that are not caught by the preceding catch blocks. This is useful for handling unexpected or unknown exceptions.
  • ExceptionType1, ExceptionType2, etc., represent the types of exceptions that can be caught. These types are usually classes derived from the std::exception class or built-in types.

If you're not directly using any of the standard exception classes provided by in your code, but your code runs without error, it's likely because these classes are indirectly included through other headers you're including.

For instance, many standard library headers include internally to provide their functionality. So, even if you don't include explicitly, you might still have access to the standard exception classes if you include headers that indirectly include .

Here's a simple example to illustrate:

#include <iostream>

int main() {
    try {
        int x = 10;
        int y = 0;
        if (y == 0) {
            throw std::runtime_error("Divide by zero error");
        }
        int result = x / y;
        std::cout << "Result: " << result << std::endl;
    } catch (std::runtime_error& e) {
        std::cout << "Exception caught: " << e.what() << std::endl;
    }
    return 0;
}

In this example, if y is zero, a std::runtime_error is thrown. The catch block catches this exception and prints an error message.

That's a basic overview of try and catch in C++. They are essential for handling errors gracefully in C++ programs.

You can also use standard exception classes provided by the C++ Standard Library. Some commonly used ones include:

std::runtime_error: Represents errors that can be detected only during runtime.
std::logic_error: Represents errors that are the result of logic errors in the program.
std::invalid_argument: Represents errors that occur due to invalid arguments.
std::out_of_range: Represents errors that occur when a value is out of its valid range.

examples for each of the standard exception classes in C++:

1- std::runtime_error:

#include <iostream>
#include <stdexcept>

int main() {
    try {
        throw std::runtime_error("A runtime error occurred");
    } catch (std::runtime_error& e) {
        std::cout << "Caught runtime error: " << e.what() << std::endl;
    }
    return 0;
}

2- std::logic_error:

#include <iostream>
#include <stdexcept>

int main() {
    try {
        throw std::logic_error("A logic error occurred");
    } catch (std::logic_error& e) {
        std::cout << "Caught logic error: " << e.what() << std::endl;
    }
    return 0;
}

3- std::invalid_argument:

#include <iostream>
#include <stdexcept>

int main() {
    try {
        throw std::invalid_argument("Invalid argument provided");
    } catch (std::invalid_argument& e) {
        std::cout << "Caught invalid argument error: " << e.what() << std::endl;
    }
    return 0;
}

4- std::out_of_range:

#include <iostream>
#include <stdexcept>

int main() {
    try {
        throw std::out_of_range("Out of range error");
    } catch (std::out_of_range& e) {
        std::cout << "Caught out of range error: " << e.what() << std::endl;
    }
    return 0;
}
⚠️ **GitHub.com Fallback** ⚠️