// exception6.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program illustrates more on how exceptions can be handled. As an example, the program // shows how the catch clause may have another try/catch clause. // Key Points: 1) Handling exceptions #include "exception6.h" ProducerConsumer::ProducerConsumer() { for (int i = 0; i < 10; i++) buff[i] = -1; } bool ProducerConsumer::isEmpty() { if (buff[0] == -1) return true; return false; } bool ProducerConsumer::isFull() { if (buff[9] != -1) return true; return false; } void ProducerConsumer::produceItem( int item ) { if (isFull()) throw ProduceOnFull(); // Not "throw ProduceOnFull;" if ( item == -1 ) throw ProduceInvalidValue(); int i = 0; while (buff[i++] != -1); buff[i - 1] = item; } void ProducerConsumer::consumeItem( ) { if (isEmpty()) throw ConsumeOnEmpty(); int i = 0, item = -1; while ( (i < 10) && (buff[i] != -1) ) i++; item = buff[i-1]; buff[i-1] = -1; } void ProducerConsumer::setItem(int item) { produceItem( item ); } void ProducerConsumer::displayBuffContents() { cout << "\nThe buffer contents at this point are: " << endl; for (int i= 0; i < 10; i++) cout << buff[i] << " "; cout << endl << endl; } void Excp::showExcpMessage() { cerr << "An exception has occurred" << endl; } void ProducerConsumerExcp::showExcpMessage() { cerr << "A producer-consumer exception has occurred" << endl; } void ConsumeOnEmpty::showExcpMessage() { cerr << "An exception has occurred: Trying to consume from an empty buffer" << endl; } void ProduceOnFull::showExcpMessage() { cerr << "An exception has occurred: Trying to produce to a full buffer" << endl; } void ProduceInvalidValue::showExcpMessage() { cerr << "An exception has occurred: Trying to produce an invalid value" << endl; } void ProduceInvalidValue::showExcpMessage2() { cout << "You will be given another chance to re-enter a correct production value. Please enter this value now: "; } void ProduceInvalidValue::showFailedTwiceMessage() { cerr << "An exception has occurred: Production failed twice due to attempts to produce ivalid values. Will give up." << endl; } int main() { ProducerConsumer pc1; try{ pc1.consumeItem( ); } catch (ConsumeOnEmpty coe) { coe.showExcpMessage(); } // Try to produce more items than the buffer can hold // Notice what happens when the exception is thrown try { for (int i = 0; i < 13; i++) { if ( i == 9 ) pc1.produceItem (-1); // Attempt to produce an invalid value pc1.produceItem(i * 5); // i*5 is just a value } } catch (ProduceOnFull pof) { pof.showExcpMessage(); } catch (ProduceInvalidValue piv) { piv.showExcpMessage(); piv.showExcpMessage2(); int v; cin >> v; try { pc1.produceItem (v); cout << "Production accepted for value " << v << ". Contents of buffer now are as follows: " << endl; } catch (ProduceInvalidValue piv) { piv.showFailedTwiceMessage(); } } catch ( ... ) { cerr <<"Your program has thrown an exception, but this exception does not seem to be handled."; cerr << " Check your code again." << endl; } pc1.displayBuffContents(); // try to consume more items than what the buffer has try { for (int i = 0; i < 13; i++) { pc1.consumeItem(); if ((i == 7) || (i ==8) || (i ==9)) // display the contents just before the pc1.displayBuffContents(); // exception should occur } } catch (ConsumeOnEmpty coe) { coe.showExcpMessage(); } catch ( ... ) { cerr <<"Your program has thrown an exception, but this exception does not seem to be handled."; cerr << " Check your code again." << endl; } return 0; } // The result of running the program (see another run below) /* An exception has occurred: Trying to consume from an empty buffer An exception has occurred: Trying to produce an invalid value You will be given another chance to re-enter a correct production value. Please enter this value now: 2 Production accepted for value 2. Contents of buffer now are as follows: The buffer contents at this point are: 0 5 10 15 20 25 30 35 40 2 The buffer contents at this point are: 0 5 -1 -1 -1 -1 -1 -1 -1 -1 The buffer contents at this point are: 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 The buffer contents at this point are: -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 An exception has occurred: Trying to consume from an empty buffer */ // Another run of the program /* An exception has occurred: Trying to consume from an empty buffer An exception has occurred: Trying to produce an invalid value You will be given another chance to re-enter a correct production value. Please enter this value now: -1 An exception has occurred: Production failed twice due to attempts to produce ivalid values. Will give up. The buffer contents at this point are: 0 5 10 15 20 25 30 35 40 -1 The buffer contents at this point are: 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 The buffer contents at this point are: -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 An exception has occurred: Trying to consume from an empty buffer */