Cse 2
Cse 2
Presented by SectorZero
Definition
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