Introduction To Exception, Try and Catch Block

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

Introduction To Exception ,Try

And Catch Block

Name:Feroz ali
Class:LH-01
USN:3NA22CS016
What is exception
• In C++, An Exception Is An Abnormal Event That Occurs During
The Execution Of A Program, Disrupting The Normal Flow Of
Control. When An Exception Is Thrown, It Can Be Caught And
Handled Using Try-catch Blocks To Provide A Controlled
Response To The Error. Exceptions In C++ Are Typically
Represented By Classes And Are Caught Using Catch Blocks
That Match The Exception Type.
Exception Handling In C++
• Exception handling in c++ is a mechanism that allows programmers to deal with
errors and abnormal situations in a more controlled and structured manner. It
provides a way to handle unexpected events that occur during the execution of a
program, such as runtime errors, exceptional conditions, or resource unavailability,

without terminating the program abruptly.

• The key components of exception handling in C++ are:


1. Throwing an Exception:
When an error or exceptional situation occurs within the program, it can throw
an exception using the throw keyword. An exception is typically an object of a
class derived from the std::exception class or any of its derived classes.
2 .Try Block:
• The code that might raise an exception is placed inside a try
block. A try block is followed by one or more catch blocks.
3 .Catch Block:
• A catch block is used to catch and handle the thrown
exception. Each catch block is associated with a specific
exception type, and it handles only exceptions of that type or
its derived types.
Here's the general syntax for exception
handling in C++:
• try {
• // Code that might throw an exception
• // ...
• } catch (ExceptionType1& e1) {
• // Exception handling code for ExceptionType1
• // ...
• } catch (ExceptionType2& e2) {
• // Exception handling code for ExceptionType2
• // ...
• } catch (...) {
• // Catch-all block (optional)
• // This block will handle any other uncaught exceptions
• // ...
• }
Example Of Exception
• #include <iostream>

• // Custom exception class


• class MyException : public std::exception {
• public:
• const char* what() const noexcept override {
• return "This is a custom exception!";
• }
• };

• int divide(int a, int b) {


• if (b == 0) {
• // Throw an exception if the divisor is zero
• throw std::runtime_error("Division by zero is not allowed!");
• }
• return a / b;
• }

• int main() {
• int num1, num2, result;

• std::cout << "Enter two numbers: ";


• std::cin >> num1 >> num2;

• try {
• // Call the divide function and catch any exceptions that occur
• result = divide(num1, num2);
• std::cout << "Result: " << result << std::endl;
• } catch (const std::runtime_error& e) {
• // Catch and handle the runtime_error exception
• std::cout << "Exception caught: " << e.what() << std::endl;
• } catch (...) {
• // Catch any other unexpected exceptions (not recommended)
• std::cout << "Unknown exception caught!" << std::endl;
• }

• return 0;
• }
• When an exception is thrown inside the try block, the program flow

immediately jumps to the first appropriate catch block whose

exception type matches the thrown exception or its base class. If no

catch block matches the thrown exception type, the catch-all block

(if present) will be executed.

• The catch block(s) handle the exception by providing appropriate

error-handling code, which may include logging the error, displaying

an error message, or taking corrective action to recover from the

exceptional situation. After the catch block executes, the program

continues with the code that follows the try-catch block.


Benefits of Exception Handling in C++:

1.Separation of Concerns: Exception handling separates error-handling logic from

normal code, leading to cleaner and more readable code.

2.Centralized Error Handling: Exceptions allow for centralized error handling,

making it easier to locate and fix issues.

3.Program Robustness: Properly handling exceptions improves the robustness of

the program by preventing abrupt terminations and enabling graceful recovery

from errors.

4.Scalability: Exception handling simplifies error management, making the code

more scalable and maintainable.


Thankyou

You might also like