C++ Exception Handling Sample Driver: TrashCan yours; TrashCan mine; TrashCan te
ID: 3939142 • Letter: C
Question
C++ Exception Handling
Sample Driver:
TrashCan yours;
TrashCan mine;
TrashCan test;
yours.setSize( 10 );
mine.setSize( 10 );
test.setSize( 5 );
yours.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
cout << test << endl;
try {
// contents will become negative...
// an exception should get thrown
test = yours - mine;
} catch( std::logic_error le ) {
cout << "subtraction failed" << endl;
}
/// test should be unchanged
cout << test << endl;
try {
// how can you have a negative size
// an exception should get thrown
test = TrashCan( -100 );
} catch( std::logic_error le ) {
cout << "constructor failed" << endl;
}
/// test should be unchanged
cout << test << endl;
try {
// how can you have 5 pieces of
// trash in a can that
// can only hold 3??
// an exception should get thrown
test.setSize( 3 );
} catch( std::logic_error le ) {
cout << "setSize failed" << endl;
}
/// test should be unchanged
cout << test << endl;
try {
// how can you have a negative
// contents value??
test = TrashCan( 10, -100 );
} catch( std::logic_error le ) {
cout << "constructor failed" << endl;
}
Explanation / Answer
TrashCan yours;
TrashCan mine;
TrashCan test;
yours.setSize( 10 );
mine.setSize( 10 );
test.setSize( 5 );
yours.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
cout << test << endl;
try {
// contents will become negative...
// an exception should get thrown
test = yours - mine;
} catch( std::logic_error le ) {
cout << "subtraction failed" << endl;
}
/// test should be unchanged
cout << test << endl;
try {
// how can you have a negative size
// an exception should get thrown
test = TrashCan( -100 );
} catch( std::logic_error le ) {
cout << "constructor failed" << endl;
}
/// test should be unchanged
cout << test << endl;
try {
// how can you have 5 pieces of
// trash in a can that
// can only hold 3??
// an exception should get thrown
test.setSize( 3 );
} catch( std::logic_error le ) {
cout << "setSize failed" << endl;
}
/// test should be unchanged
cout << test << endl;
try {
// how can you have a negative
// contents value??
test = TrashCan( 10, -100 );
} catch( std::logic_error le ) {
cout << "constructor failed" << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.