[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng] / [R J Botting] / [CSci202] / lab08
[Text Version] [Syllabus] [Schedule] [Glossary] [Resources] [Grading] [Contact] [Question] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Thu May 20 08:00:36 PDT 2010


    CSci202 Laboratory 08 Exceptions

      Don't forget to check the latest news [ http://cse.csusb.edu/dick/cs202/ ] for this class.


      Complete [ lab04.html ] before you start this laboratory.


      Learn about C++ exceptions and exception handling

      Text Chapter 17 Exception Handling


      Making a good reusable functions and classes means you have to harden them against abuse by their users. Raising exceptions is the modern way to do this.

      Handling exceptions is a good skill to learn as more and more functions and classes throw exceptions when things go wrong.


      1. Start by making a back up copies of your working
        Just In Case. Continue working with the originals.

        By the way -- the UNIX/Linux copy command is cp and can be used like this:

         		cp buffer04.h buffer04.h.bak
      2. Make sure that lab04main.cpp still compiles!
      3. Add the
        needed (book page 678) to both files. Include
         	using namespace std;
        Test the result with a quick recompilation and run of lab04main.cpp!
      4. Currently the operator[] returns '\0' when the index i is out of range. Instead make it throw out_of_range("Buffer operator[]").
      5. Test main program with out changing it.
      6. If needed add a new command in main program to execute
         		cout<< b[24]<<endl;
      7. Run it and watch the exception get thrown.
      8. In the main program add a try{...}catch(...){...} about the command that fails to catch the out_of_range exception. Here is a suggested test( page 352)
         		assert( b[24] == '\0' );
         	catch(exception& e)
         		cerr << "Exception "<< typeid(e).name() << ": " <<  e.what();
      9. In buffer04.h add a variable to count the number of discarded characters. You need two new lines
         		int discard=0;
        somewhere. Figure out where inside operator>>.
      10. Test by compiling and running lab04main.cpp.
      11. Add a throw to the operator>> function in the buffer04.h so that if (discard >0) then throw a 'length_error'. Compile lab04main as is and test with short and long inputs.
      12. Change lab04main so that it catches the length error and reports it to the user. Note: you should be able to catch errors in both cin>> commands with a single
        block. Hint: put the try{ at the start and catch(...){...} at the end of the main block.

      13. Add another throw to the operator>> function to recognize enemy action: more than 70 characters in a line. It should throw
         		runtime_error("Hacker Attacker!");
      14. Test the lab04main.cpp program to be sure that it distinguishes a mistake from a likely attack. Add a catch(exception&e)... after the catch(length_error&e){...}.
      15. Show me that the program runs and handles all three exceptions and earn lots of points.
      If you finish early, work on next project.

    (next): Information Security [ lab09.html ]


  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. class::="A description of a set of similar objects that have similar data plus the functions needed to manipulate the data".
  3. Data_Structure::=A small data base.
  4. Function::programming=A selfcontained and named piece of program that knows how to do something.
  5. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  6. KDE::="Kommon Desktop Environment".
  7. object::="A little bit of knowledge -- some data and some know how", and instance of a class".
  8. OOP::="Object-Oriented Programming", Current paradigm for programming.
  9. Semantics::=Rules determining the meaning of correct statements in a language.
  10. SP::="Structured Programming", a previous paradigm for programming.
  11. STL::="The standard C++ library of classes and functions" -- also called the "Standard Template Library" because many of the classes and functions will work with any kind of data.
  12. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  13. Q::software="A program I wrote to make software easier to develop",
  14. TBA::="To Be Announced", something I should do.
  15. TBD::="To Be Done", something you have to do.
  16. UML::="Unified Modeling Language".
  17. void::C++Keyword="Indicates a function that has no return".