

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A lab exercise for csi 1440 students during the summer 2005 semester. The lab focuses on exception handling in c++ by teaching students how to throw and catch exceptions, declare exception classes, and use them in a program. Students are required to create a 'exceptions.cpp' file, define three classes (exlist, monstertruck, and breakable), and implement exception handling for various scenarios.
Typology: Lab Reports
1 / 3
This page cannot be seen from the preview
Don't miss anything!
Summer 2005
In this lab we will learn how to throw exceptions, how to catch exceptions, and how to declare one class inside another.
If a function throws an exception, it will execute a statement like the following:
throw
Where
An exception can be caught or ignored. If it is ignored, the program will normally be terminated when it occurs. An exception is caught by using the following construct.
try { functions or other statements that might throw exceptions. } catch (char * message) { exception handler for char * (value will be used) } catch (int x) { exception handler for int (value will be used) } catch (MyClass) { exception handler for the exception class MyClass. (value will be ignored) }
Create a program called “Exceptions.cpp”.
In your program create three classes, exList, MonsterTruck, and Breakable. The class exList will be a container class for four exception classes named Ex1, Ex2, Ex3, and Ex4. Declare this class as follows: class exList { public: class Ex1 {}; class Ex2 {}; class Ex3 {}; class Ex4 {}; };
Summer 2005
Note that exception classes are very often empty classes. A constant of type exList::Ex1() is used to throw an exception of type Ex1 from class exList.
MonsterTruck will be an exception class with two public members, int a, and int b. MonsterTruck will have a default constructor that initializes a and b to zero and an int,int constructor that initializes both a and b to user-specified values. No other functions are required for this class.
The class Breakable will be a regular class with three public data items int a,b,c, and two public functions int func1(int x) and int func2(int x). No other functions are required for this class. The function func1 will have the body:
cout<<"func1 Body\n"; a=x; b=x+1; return a+b;
The function func2 will have the body:
cout<<"func2 Body\n"; c=x; b=x+1; return b+c;
The class Breakable will have two internal exception classes, Ex1 and Ex2, defined like Ex1 and Ex2 of exList. (It is OK for them to have the same names.)
The function func1 of class Breakable will throw Ex1( ) if its argument is equal to 7, exList::Ex1( ) if its argument is equal to 8, and exList::Ex2( ) if its argument is equal to 9.
The function func2 of class Breakable will throw Ex2( ) if its argument is equal to 9, “Hey! Stop That!” if its argument is equal to 10, and MonsterTruck(2,3) if its argument is equal to 11.
The main routine of the program will declare a variable B of type Breakable, and will call B.func1( ) and B.func2( ) 12 times with arguments from 0 through 11, using the following loop.
for (k=0 ; k<12 ; k++) { cout<<k<<endl; try { B.func1(k); B.func2(k); } catch … }