0% found this document useful (0 votes)
11 views5 pages

Experiment 11

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Experiment 11

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Page No.

Experiment No.:-11 Date:__________


Aim : To study Exception Handling

Theory:

Exception handling in C++ is a critical aspect of programming that enables


developers to manage runtime errors and unexpected conditions effectively. It
provides a structured mechanism for responding to exceptional situations, allowing
programs to continue execution or terminate gracefully without crashing.

1. Core Concepts
At the heart of C++ exception handling are three key components: try, catch, and
throw.

● try: A block of code that may produce an exception is enclosed within a try
block. When an error occurs, the normal flow of execution is interrupted.
● catch: This block follows a try block and defines how to respond to a
particular type of exception. If an exception is thrown in the try block, control is
transferred to the appropriate catch block.
● throw: This keyword is used to signal that an exception has occurred. It can
throw various types, including built-in data types and user-defined objects.

2. Mechanism of Exception Handling

When an exception is thrown, the runtime system searches for a corresponding


catch block that can handle that specific exception type. If such a block is found, its
code is executed; if not, the program will terminate, possibly after unwinding the
stack. This behavior ensures that resources can be cleaned up properly, maintaining
program stability.

3. Multiple Catch Blocks

C++ supports the use of multiple catch blocks, enabling different responses based
on the type of exception thrown. This allows for precise handling of various error
conditions.

4. Catch-All Handler

A catch-all handler can be implemented using an ellipsis (...). This special catch
block can capture any exception not matched by preceding catch blocks, providing a
fallback mechanism for unhandled exceptions.

5. Re-throwing Exceptions

Exception Handling Name Roll No


Page No.

Within a catch block, exceptions can be re-thrown using the throw; statement. This
feature allows for additional error handling, such as logging or cleanup, before the
exception is passed up the call stack for further handling.

6. Standard Exception Classes

The C++ Standard Library includes a hierarchy of predefined exception classes,


primarily found in the <stdexcept> header. Key classes include std::exception,
std::runtime_error, std::logic_error, and std::out_of_range. These classes facilitate
robust error handling by providing standardized ways to represent various error
conditions.

Exception Handling Name Roll No


Page No.

Program 11[A]:Write a C++ program to implement exceptional handling concept


(Divide by zero) using exception rethrow mechanism

Program: Output:

Exception Handling Name Roll No


Page No.

Program 11[B]:Write a C++ program to implement a multi catch exception handling


mechanism

Program: Output:

Exception Handling Name Roll No


Page No.

Program 11[C]:Your code

Program: Output:

Conclusion:

Exception Handling Name Roll No

You might also like