You are on page 1of 10

PROGRAM 1

#include<iostream>

using namespace std;

int main()

int height;

cin >> height;

try

if (height > 300)

throw "height exceeds maximum";

if (height < 30)

throw "height below minimum";

cout << "Person is " <<height<< "inches tall" << endl;

catch(const char msg[])

cout << "Exception occured: "<<

msg << endl;

cout << "Program Stops " << endl;

return 0;

PROGRAM 2

#include<iostream>

using namespace std;

int ToInches (int cm)


{

if (cm == 100)

throw "you are a winner!";

return cm/2.54;

int main()

int height;

cin >> height;

try

if (height > 300)

throw "height exceeds maximum";

if (height < 30)

throw "height below minimum";

cout << "Person is " <<ToInches(height)

<< "inches tall" << endl;

catch(const char msg[])

cout << "Exception occured: "<<

msg << endl;

cout << "Program Stops " << endl;

return 0;

}
PROGRAM 3

#include<iostream>

using namespace std;

int ToInches (int cm)

if (cm == 100)

throw "you are a winner!";

return cm/2.54;

int main()

int height;

cin >> height;

try

if (height <= -1)

throw 0;

if (height > 300)

throw "height exceeds maximum";

if (height < 30)

throw "height below minimum";

cout << "Person is " <<ToInches(height)

<< "inches tall" << endl;

catch (int i)

cout << "Bad input: height cannot be less than " << i << endl;

catch(const char msg[])

{
cout << "Exception occured: "<< msg << endl;

PROGRAM 4

#include<iostream>

using namespace std;

int main()

string s="yadda yadda";

int input;

cin >>input;

try

if (input == 1) throw 1.1;

if (input == 2) throw 2;

if (input == 3) throw "bla bla";

if (input == 4) throw s;

catch (double i)

{ cout << "Double exception handler" << endl;

catch (int i)

{ cout << "Integer exception handler" << endl;

catch(const char msg[])

{ cout<<"char array exception handler" <<endl;

catch(string s)

{ cout << "String exception handler" << endl;

}
PROGRAM 5

#include<iostream>

using namespace std;

int ToInches (int cm)

if (cm == 100)

throw "you are a winner!";

return cm/2.54;

int main()

int height;

string err="Input cannot be below 0";

try

cin >> height;

try

if (height <= -1)

throw err;

if (height > 300)

throw "height exceeds maximum";

if (height < 30)

throw height;

cout << "Person is " << ToInches(height) << " inches tall" << endl;

catch(const char msg[])

cout << "Exception occured: " << msg << endl;


}

cout << "I am in the middle.\n" << endl;

catch (int i)

cout << "Exception occured: Height must be greater than "<<i<< endl;

PROGRAM 6

#include <iostream>

//#include <exception>

using namespace std;

class Divide_By_Zero_Exception : public exception{

public:

const char * what() const throw()

return "Divide By Zero Exception\n";

};

int main()

try

int a, b;

cout << "Enter two numbers : ";

cin >> a >> b;

// compute a / b
if (b == 0)

Divide_By_Zero_Exception d;

throw d;

else

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

catch(exception& e)

cout << e.what();

PROGRAM 7

#include <iostream>

#include <exception>

using namespace std;

class Array_bound_Exception : public exception{

public:

const char * what() const throw()

return "Array bound exceeds\n";

};

int main()
{

try

int a[10],i;

cout << "Enter the numbers : ";

for(i=0;i<=100;i++)

/* if (i>9)

{*/

Array_bound_Exception d;

throw d;

//}

cin >>a[i];

for(i=0;i<=9;i++)

cout<<a[i]<<endl;

catch(exception& e)

cout << e.what();

PROGRAM 8

#include <iostream>

using namespace std;


struct E {

const char* message;

E(const char* arg) : message(arg) { }

};

void my_terminate() {

cout << "Call to my_terminate" << endl;

};

struct A {

A() { cout << "In constructor of A" << endl; }

~A() {

cout << "In destructor of A" << endl;

throw E("Exception thrown in ~A()");

};

struct B {

B() { cout << "In constructor of B" << endl; }

~B() { cout << "In destructor of B" << endl; }

};

int main() {

set_terminate(my_terminate);

try {

cout << "In try block" << endl;

A a;

cout<<"Inside A"<<endl;

B b;

throw("Exception thrown in try block of main()");


cout<<"Inside B"<<endl;

catch (const char* e) {

cout << "Exception: " << e << endl;

catch (...) {

cout << "Some exception caught in main()" << endl;

cout << "Resume execution of main()" << endl;

You might also like