0% found this document useful (0 votes)
20 views

Cse 2

Exception handling in C++ uses try, catch, and throw statements to deal with errors or exceptions in a structured way. The try block contains code that might throw exceptions, catch blocks catch specific exception types, and throw statements throw exceptions. Exceptions can be thrown within or outside of try blocks and caught by catch blocks within the same or nested functions. Multiple catch blocks can handle different exception types, and a catch-all block can handle any exceptions not caught previously.

Uploaded by

lucifesk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Cse 2

Exception handling in C++ uses try, catch, and throw statements to deal with errors or exceptions in a structured way. The try block contains code that might throw exceptions, catch blocks catch specific exception types, and throw statements throw exceptions. Exceptions can be thrown within or outside of try blocks and caught by catch blocks within the same or nested functions. Multiple catch blocks can handle different exception types, and a catch-all block can handle any exceptions not caught previously.

Uploaded by

lucifesk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Exception Handling

Presented by SectorZero
Definition

A built-in error handling mechanism in C++ that deals with errors or


exceptional situations in a program in a structured and controlled
way.
Exception Handling

Key points:
• “ try ” Block

• “ catch “ Block

• “ throw “ statement
What’s the core syntax?
Basic Exception Handling
#include <iostream >
using namespace std;
int main()
{
cout << " start \n";
try // start a try block
{
cout << " Inside try block \n";
throw 10; // throw an error
cout << " This will not execute ";
}
catch (int i) // catch an error
{
cout << " Caught One ! Number is: ";
cout << i << "\n";
}
cout << "end ";
return 0;
}
What if the type of exception does not match the type specified
in the catch statement?
// This will not work .
#include <iostream >
using namespace std;
int main()
{
cout << " start \n";
try // start a try block
{
cout << " Inside try block \n";
throw 10; // throw an error
cout << " This will not execute ";
}
catch (double i) // won 't work for an int exception
{
cout << " Caught One ! Number is: ";
cout << i << "\n";
}
cout << "end ";
return 0;
}
Can an exception be thrown outside a try block?
#include <iostream >
using namespace std;
void Xtest(int test)
{
cout << " Inside Xtest , test is: " << test << "\n";
if (test)
throw test;
}
int main()
{
cout << " start \n";
try // start a try block
{
Xtest(0);
Xtest(1);
Xtest(2);
}
catch (int i) // catch an error
{
cout << " Caught One ! Number is: ";
cout << i << "\n";
}
cout << "end ";
return 0;
}
What happens if try block localized to a function?
#include <iostream >
using namespace std;
// A try / catch can be inside a function other than main ().
void Xhandler(int test)
{
try
{
if (test)
throw test;
}
catch (int i)
{
cout << " Caught One ! Ex , #: " << i << '\n';
}
}
int main()
{
cout << " start \n";
Xhandler(1);
Xhandler(2);
Xhandler(0);
Xhandler(3);
cout << "end ";
return 0;
}
How to use multiple catch blocks?
#include <iostream >
using namespace std;
void Xhandler(int test)
{
try
{
if (test)
throw test;
else
throw " Value is zero .";
}
catch (int i)
{
cout << " Caught One ! Ex , #: " << i << '\n';
}
catch (const char *str)
{
cout << " Caught a string : ";
cout << str << '\n';
}
}
int main()
{
cout << " start \n";
Xhandler(1);
Xhandler(2);
Xhandler(0);
Xhandler(3);
cout << "end ";
return 0;
}
How to use catch block as default?
#include <iostream >
using namespace std;
void Xhandler(int test)
{
try
{
if (test == 0)
throw test; // throw int
if (test == 1)
throw 'a'; // throw char
if (test == 2)
throw 123.23; // throw double
}
catch (int i) // catch an int exception
{
cout << " Caught " << i << '\n';
}
catch (...) // catch all other exceptions
{
cout << " Caught One !\n";
}
}
int main()
{
cout << " start \n";
Xhandler(0);
Xhandler(1);
Xhandler(2);
cout << "end ";
return 0;
}
Exception Handling

Conclusion:
Exception handling in C++ plays a pivotal role in ensuring the robustness and reliability of our programs. As we

explored the try, catch, and throw statements, we witnessed how these constructs empower developers to gracefully

handle runtime errors and unexpected situations. Exception handling is not merely a mechanism for error

management; it's a fundamental tool for building resilient software.


THANK YOU

You might also like