0% found this document useful (0 votes)
47 views5 pages

C++ Exception Handling Example Code

This document discusses exception handling in C++ using try, catch, and throw. It includes examples of throwing different data types from functions and constructors, and catching exceptions of various types. There are also examples of dividing by zero and memory allocation errors.

Uploaded by

Mihaela Guja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views5 pages

C++ Exception Handling Example Code

This document discusses exception handling in C++ using try, catch, and throw. It includes examples of throwing different data types from functions and constructors, and catching exceptions of various types. There are also examples of dividing by zero and memory allocation errors.

Uploaded by

Mihaela Guja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <iostream>

#include <iomanip>
#include <cstdlib>
#include <ctime>

#include <string>
#include <cstring>

#include <exception>

using namespace std;

/*

try , catch , throw

try
{

....
cod vulnerabil
....

}
catch( tip_date )
{
....
}
catch( tip_date )
{
....
}
catch( tip_date )
{
....
}
catch( tip_date )
{
....
}

*/

void f()
{
cout<<" f() "<<endl;
throw 123;
}
class abc
{
public:
abc(){ throw 777.888; }
};

class ERR : public exception


{

public:

char* what() const throw(){ return "Mesaj de eroare"; }

}XXX;

int main()
{
srand(time(0));

try
{
throw XXX;
}
catch(exception &e)
{
cout<<" ERR Eroare :: "<<e.what()<<endl;
}

/*
try
{
int *p = new int[99999999999];

cout<<" Sa alocat cu succes"<<endl;


}
catch(bad_alloc &e)
{
cout<<" bad_alloc Eroare :: "<<e.what()<<endl;
}
catch(exception &e)
{
cout<<" exception Eroare :: "<<e.what()<<endl;
}
catch(...)
{
cout<<" exception Eroare "<<endl;
}
*/

/*
char sir[256]="step";

string S="itSTEP";

try
{
// cod

int x;

x=3;

throw 123;

if( x == 7 ) throw S;
if( x == 6 ) throw sir;
if( x == 5 ) throw "Eroare";
if( x == 4 ) throw 'a';
if( x == 3 ) throw 3.14;
if( x == 2 ) throw 123;
if( x < 2 ) throw true;

// cod

}
catch(bool e)
{
cout<<" Exceptie bool = "<<e<<endl;
}
catch(int e)
{
cout<<" Exceptie int = "<<e<<endl;
}
catch(double e)
{
cout<<" Exceptie double = "<<e<<endl;
}
catch(char e)
{
cout<<" Exceptie char = "<<e<<endl;
}
catch(char e[])
{
cout<<" Exceptie char[] = "<<e<<endl;
}
catch(const char e[])
{
cout<<" Epxceptie const char[] = "<<e<<endl;
}
catch(string e)
{
cout<<" Epxceptie string = "<<e<<endl;
}
catch(...)
{
cout<<" Epxceptie ... "<<endl;
}

*/
/*
int a,b, i=0;

while( i < 10 )
{

try
{
a=rand()%5;
b=rand()%5;

if( b == 0 ) throw " Eroare impartirea la ZERO";

cout<<i<<" a="<<a<<" b="<<b<<" a/b= "<<a/b<<endl;


}
catch( const char e[])
{
cout<<i<<" xxx a="<<a<<" b="<<b<<" a/b= "<<e<<endl;
}
catch(const char[])
{

b=rand()%10+10;

cout<<i<<" xxx a="<<a<<" b="<<b<<" a/b= "<<(double)a/b<<endl;


}
catch(...)
{
cout<<" Erorare "<<endl;
}

i++;
}

*/

/*
try
{
f();
}
catch(double e)
{
cout<<" double f() = "<<e<<endl;
}
catch(int e)
{
cout<<" int f() = "<<e<<endl;
}

*/
/*
try
{
abc A;
}
catch(double e)
{
cout<<" double abc OBJECT = "<<e<<endl;
}
catch(int e)
{
cout<<" int abc OBJECT = "<<e<<endl;
}

*/

// sqrt X de n ori X => cu valor random -10 ... 10

return 0;
}

You might also like