DIFFERENT EXCEPTIONAL HANDLING PROGRAMS
BAD-ALLOC EXAMPLE:-
// bad_alloc example
#include // std::cout
#include // std::bad_alloc
int main () {
try
{
int* myarray= new int[10000];
}
catch (std::bad_alloc& ba)
{
std::cerr << "bad_alloc caught: " << ba.what() << '\n';
}
return 0;
}
BAD-CAST EXAMPLE:-
// bad_cast example
#include // std::cout
#include // std::bad_cast
class Base {virtual void member(){}};
class Derived : Base {};
int main () {
try
{
Base b;
Derived& rd = dynamic_cast(b);
}
catch (std::bad_cast& bc)
{
std::cerr << "bad_cast caught: " << bc.what() << '\n';
}
return 0;
}
BAD-EXCEPTION EXAMPLE:-
//Exception Handling: Use of built-in exception class
#include
#include //header file of exception class
using namespace std;
int main()
{
int *a;
long n;
cout<<"\nEnter no. of values:";
cin>>n;
try
{
a = new int[n]; //exception will be thrown by default if the n exceeded memory limit
cout<<"Array created in memory for "<<n<<" elements.";
delete a;
}
catch(bad_alloc e) //or catch (exception e)
{
cout<<"\n** Error: "<<e.what();
}
}
INVALID-ARGUMENT EXAMPLE:-
// invalid_argument example
#include // std::cerr
#include // std::invalid_argument
#include // std::bitset
#include // std::string
int main (void) {
try {
// bitset constructor throws an invalid_argument if initialized
// with a string containing characters other than 0 and 1
std::bitset mybitset (std::string("01234"));
}
catch (const std::invalid_argument& ia) {
std::cerr << "Invalid argument: " << ia.what() << '\n';
}
return 0;
}
STANDARD EXCEPTION EXAMPLE:-
// standard exception
#include
#include
using namespace std;
int main () {
try
{
int* myarray= new int[1000];
}
catch (exception& e)
{
cout << "Standard exception: " << e.what() << endl;
}
return 0;
}
LENGTH ERROR EXAMPLE:-
// length_error example
#include // std::cerr
#include // std::length_error
#include // std::vector
int main (void) {
try {
// vector throws a length_error if resized above max_size
std::vector myvector;
myvector.resize(myvector.max_size()+1);
}
catch (const std::length_error& le) {
std::cerr << "Length error: " << le.what() << '\n';
}
return 0;
}
LOGICAL ERROR EXAMPLE:-
//range error when input value a=
#include
using namespace std;
int main()
{
int a, b, c;
cout<<"\nEnter two numbers: ";
cin>>a>>b;
try
{
if(b==0) throw 0;
c = a/b;
cout<<"\nDivision result is: "<<c;
}
catch(int)
{
cout<<"\nError: out of range of integer";
}
}
OUT OF RANGE EXAMPLE:-
// out_of_range example
#include // std::cerr
#include // std::out_of_range
#include // std::vector
int main (void) {
std::vector myvector(10);
try {
myvector.at(20)=100; // vector::at throws an out-of-range
}
catch (const std::out_of_range& oor) {
std::cerr << "Out of Range error: " << oor.what() << '\n';
}
return 0;
}
RUN-TIME ERROR:-
//Basic Use of try-catch
#include
using namespace std;
int main()
{
int a, b, c;
cout<<"\nEnter two numbers: ";
cin>>a>>b;
try
{
if(b==0) throw 0;
c = a/c;
cout<<"\nDivision result is: "<<c;
}
catch(int)
{
cout<<"\nError: Divide by Zero";
}
}
EXCEPTION MULTIPLE CATCH BLOCKS:-
//Exception handling: Multiple catch blocks
#include
using namespace std;
int main()
{
int a, b, c;
cout<<"\nEnter two numbers: ";
cin>>a>>b;
try
{
if(a==0 and b==0) throw (float)0;
if(b==0) throw (int)0;
c = a/b;
cout<<"\nDivision result is: "<<c;
}
catch(int)
{
cout<<"\nError: Divide by Zero: Result is INFINITY";
}
catch(float)
{
cout<<"\nError: Divide by Zero: Result is NaN";
}
}
SINGLE CATCH BLOCK EXAMPLE TO HANDLE MULTIPLE ERROR:-
//Exception handling: single catch block to handle multiple errors using error codes
#include
using namespace std;
int main()
{
int a, b, c;
cout<<"\nEnter two numbers: ";
cin>>a>>b;
try
{
if(a==0 and b==0) throw 2;
if(b==0) throw 1;
c = a/b;
cout<<"\nDivision result is: "<<c;
}
catch(int& errorcode) //can also work without &
{
if(errorcode==1)
cout<<"\nError: Divide by Zero: Result is INFINITY";
else
cout<<"\nError: Divide by Zero: Result is NaN";
}
}
USE OF BUILT-IN EXCEPTION CLASS:-
//Exception Handling: Use of built-in exception class
#include
#include //header file of exception class
using namespace std;
int main()
{
float a, b;
cout<<"\nEnter two numbers:";
cin>>a>>b;
try
{
if(b==0) throw exception();
cout<<"Division is "<<a/b;
}
catch(exception e)
{
cout<<"\n** Error: "<<e.what();
}
}
USER-DEFINED EXCEPTION CLASS:-
//Exception Handling: user-defined exception class
#include
#include //header file of exception class
using namespace std;
class DivByZero: public exception
{
public:
//override the what function declared in exception class with header as:
// virtual const char* what() const throw() { }
//or simply with th header as under:
char* what()
{
return "Divide by Zero."; //error message that you want to send
}
};
int main()
{
float a, b;
cout<<"\nEnter two numbers:";
cin>>a>>b;
try
{
if(b==0) throw DivByZero();
cout<<"Division is "<<a/b;
}
catch(DivByZero e)
{
cout<<"\n** Error: "<<e.what();
}
}

ITS VERY HELP FULL
ReplyDelete