Exceptions (repost)

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 (repost)

 
0
  #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?


// Fig. 13.1: fig13_01.cpp
// A simple exception-handling example that checks for
// divide-by-zero exceptions.


  1.  
  2. #include <iostream>
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6. #include <exception>
  7. using std::exception;
  8. // DivideByZeroException objects should be thrown by functions
  9. // upon detecting division-by-zero exceptions
  10. class DivideByZeroException : public exception {
  11. public:
  12. // constructor specifies default error message
  13. DivideByZeroException::DivideByZeroException()
  14. : exception( "attempted to divide by zero" ) {}
  15. }; // end class DivideByZeroException
  16. // perform division and throw DivideByZeroException object if
  17. // divide-by-zero exception occurs
  18. double quotient( int numerator, int denominator )
  19. {
  20. // throw DivideByZeroException if trying to divide by zero
  21. if ( denominator == 0 )
  22. throw DivideByZeroException(); // terminate function
  23. // return division result
  24. return static_cast< double >( numerator ) / denominator;
  25. } // end function quotient
  26. int main()
  27. {
  28. int number1; // user-specified numerator
  29. int number2; // user-specified denominator
  30. double result; // result of division
  31. cout << "Enter two integers (end-of-file to end): ";
  32. // enable user to enter two integers to divide
  33. while ( cin >> number1 >> number2 ) {
  34.  
  35. // try block contains code that might throw exception
  36. // and code that should not execute if an exception occurs
  37. try {
  38. result = quotient( number1, number2 );
  39. cout << "The quotient is: " << result << endl;
  40. } // end try
  41. // exception handler handles a divide-by-zero exception
  42. catch ( DivideByZeroException &divideByZeroException ) {
  43. cout << "Exception occurred: " <<
  44. divideByZeroException.what() << endl;
  45. } // end catch
  46. cout << "\nEnter two integers (end-of-file to end): ";
  47. } // end while
  48. cout << endl;
  49. return 0; // terminate normally
  50. } // end main
Here is the instructions and code I'm trying to use.


/*Create a program that defines three user defined exception classes
(called ExceptionOne, ExceptionTwo, and ExceptionThree
- with the messages being the same as the name - see Figure 13.1 for an example).
The program should have a loop with a menu with four selections
- Option 1 will throw ExceptionOne, Option 2 ExceptionTwo, Option 3 ExceptionThree
, and Option 4 to exit.
The catch block should only catch the first two exceptions explicitly
with the last exception caught generically.
Make sure the switch logic is inside a try block followed by three catch blocks.
///////////////////////////////////////////////////////////////////////////////

  1.  
  2. #include <iostream>
  3. #include <exception>
  4. #include <ctime>
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8.  
  9. class BaseException
  10. {
  11. public:
  12. // constructor specifies default error message
  13. ExceptionOne::BaseException ()
  14. : ExceptionOne
  15. }// end class
  16. class ExceptionOne: public BaseException {
  17. public:
  18. ExceptionOne::ExceptionOne()
  19. :ExceptionOne
  20. }// end class
  21.  
  22.  
  23. class ExceptionTwo: public ExceptionOne {
  24. public:
  25. // constructor specifies default error message
  26. ExceptionTwo::ExceptionTwo()
  27. : ExceptionTwo
  28. } // end class
  29. class ExceptionThree: public ExceptionTwo {
  30. public:
  31. ExceptionThree::ExceptionThree()
  32. :ExceptionThree
  33. }// end class
  34.  
  35.  
  36. int main()
  37. {

Any help greatly appreciated. Thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 331
Moderator
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Exceptions (repost)

 
0
  #2
Feb 6th, 2007
Please use proper indentation for your code. Doing it is a good habit, and most editors have an option that enables automatic indentation. That way we can read your code eaiser.

>I thought that catching problems was up to the compiler.

It is, but not entirely. A compiler can only catch syntax errors, not runtime errors. For example, consider the following equation:

  1. x=5/0;
Now, most compilers will automatically alert you of this error because of course, you cannot divide by 0. This is a syntax error. However, consider this statement:
  1. x=5/divider;
The compiler has no way of knowing what divider will contain at runtime. It could have been inputted by the user, been generated as a random number, etc.. There's many other runtime errors that the compiler cannot predict, such as the computer having insufficient memory, a file being missing; you get the idea. So then it's up to your program to handle the errors.

That's where exception handling comes in. It allows you an easy way to control and handle errors, as opposed to the traditional C-style of error checking, where you decided a function's success purely based on return code or values of pointers modified.

You could just throw a simple string, but using classes gives you much more flexibility. You can fill in data such as where the error occured, the date and time, why it occured. Additionally, using classes allows you to take advantage of exception handling's type checking, which will automatically pick out the right catch block to use.
"Technological progress is like an axe in the hands of a pathological criminal."
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