// exception3.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program introduces a standard language-level facility for // exception handling. This facility provides a uniform syntax and style, // yet it allows programmers to perform fine-tuning should they wish. // The exception-handling facility can significantly reduce the size and // complexity of program code by eliminating the need for testing anomalous // states explicitly. // The try block begins with the "try" keyword followed by a sequence of // program statements enclosed within braces. Following the "try" block // is a list of handlers; these are the "catch" clauses. // Key Points: 1) Exception-handling facility // 2) The "try" block // 3) The "catch" handler and the "throw" clause #include "exception3.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;" 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; } 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++) { pc1.produceItem(i * 5); // i*5 is just a value if ((i == 7) || (i ==8) || (i ==9)) // just show the buffer contents at these three points pc1.displayBuffContents(); } } 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 buffer contents at this point are: 0 5 10 15 20 25 30 35 -1 -1 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 5 10 15 20 25 30 35 40 45 An exception has occurred: Trying to produce to a full buffer The buffer contents at this point are: 0 5 10 15 20 25 30 35 40 45 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 */