Introduction To Exception, Try and Catch Block
Introduction To Exception, Try and Catch Block
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,
• int main() {
• int num1, num2, result;
• 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
catch block matches the thrown exception type, the catch-all block
from errors.