Exceptions

Reply

Join Date: Sep 2006
Posts: 38
Reputation: CurtisBridges is on a distinguished road 
Solved Threads: 1
CurtisBridges CurtisBridges is offline Offline
Light Poster

Exceptions

 
-1
  #1
Feb 6th, 2007
Exceptions
I am working on a class project as layed out by the instructor. This is in conjunction with Chapter 13 (Exceptions) from the book How To Program in C++ by Deitle 4/e. I am at a total loss on this subject. I thought that catching problems was up to the compiler. I have included Figure 13.1 for an example as per instructions and some code I have been working on. Could someone please look this over and help me get my brain around exceptions or at least this project?

  1. // Fig. 13.1: fig13_01.cpp
  2. // A simple exception-handling example that checks for
  3. // divide-by-zero exceptions.
  4. Code:
  5. #include <iostream>using std::cout;using std::cin;using std::endl;#include <exception>using std::exception;// DivideByZeroException objects should be thrown by functions// upon detecting division-by-zero exceptionsclass DivideByZeroException : public exception {public: // constructor specifies default error message DivideByZeroException::DivideByZeroException() : exception( "attempted to divide by zero" ) {}}; // end class DivideByZeroException// perform division and throw DivideByZeroException object if // divide-by-zero exception occursdouble quotient( int numerator, int denominator ){ // throw DivideByZeroException if trying to divide by zero if ( denominator == 0 ) throw DivideByZeroException(); // terminate function // return division result return static_cast< double >( numerator ) / denominator;} // end function quotientint main(){ int number1; // user-specified numerator int number2; // user-specified denominator double result; // result of division cout << "Enter two integers (end-of-file to end): "; // enable user to enter two integers to divide while ( cin >> number1 >> number2 ) { // try block contains code that might throw exception // and code that should not execute if an exception occurs try { result = quotient( number1, number2 ); cout << "The quotient is: " << result << endl; } // end try // exception handler handles a divide-by-zero exception catch ( DivideByZeroException &divideByZeroException ) { cout << "Exception occurred: " << divideByZeroException.what() << endl; } // end catch cout << "\nEnter two integers (end-of-file to end): "; } // end while cout << endl; return 0; // terminate normally} // end main


Here is the instructions and code I'm trying to use.

  1. /*Create a program that defines three user defined exception classes
  2. (called ExceptionOne, ExceptionTwo, and ExceptionThree
  3. - with the messages being the same as the name - see Figure 13.1 for an example).
  4. The program should have a loop with a menu with four selections
  5. - Option 1 will throw ExceptionOne, Option 2 ExceptionTwo, Option 3 ExceptionThree
  6. , and Option 4 to exit.
  7. The catch block should only catch the first two exceptions explicitly
  8. with the last exception caught generically.
  9. Make sure the switch logic is inside a try block followed by three catch blocks.
  10. ///////////////////////////////////////////////////////////////////////////////*/
  11. Code:
  12. #include <iostream>#include <exception>#include <ctime>using std::cout;using std::cin;using std::endl;class BaseException {public: // constructor specifies default error message ExceptionOne::BaseException () : ExceptionOne }// end class class ExceptionOne: public BaseException {public: ExceptionOne::ExceptionOne() :ExceptionOne}// end classclass ExceptionTwo: public ExceptionOne {public: // constructor specifies default error message ExceptionTwo::ExceptionTwo() : ExceptionTwo} // end class class ExceptionThree: public ExceptionTwo {public: ExceptionThree::ExceptionThree() :ExceptionThree}// end classint main(){


Any help greatly appreciated. Thanks
Last edited by WaltP; Feb 6th, 2007 at 2:56 pm. Reason: Added CODE tags -- not that it helped any...
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Exceptions

 
0
  #2
Feb 6th, 2007
Perhaps learn how to use [code][/code] tags as well.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,027
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 125
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Exceptions

 
0
  #3
Feb 6th, 2007
Um ... yeah, that's readable. Perhaps you could repost your code using line breaks?
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC