// exception4.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program gives more information about exception handling. // In specific, this program illustrates what happens when you have an // unhandled exception. In fact, the program crashes as a result of unhandled // exception. The next program, exception5.cpp will show one of the possible // solutions to avoid such problems. #include "exception4.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; } 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(); } 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(); } return 0; } // The result of running the program /* An exception has occurred: Trying to consume from an empty buffer // The program terminates abnormally (crashes) at this moment */