Skip to main contentCal State San Bernardino / [CNS] / [Comp Sci Dept] / [R J Botting] >> [CSci202] >> 09
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Contact] [Grading]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Tue May 1 10:48:54 PDT 2007

Contents


    CSci202 Computer Science II, Session 09 Exceptions


      (Previous): OO Models [ 08.html ]

      Preparation

      Study this page and Chpater 10 on Exceptions in C++ and write down the questions, doubts, and surprises that you have on a piece of paper. Include your printed name!

      Assigned Work Due

      Hand in your questions, doubts and surprises.

      Question: would anyone like the option of Emailing there question... to me before class?

      Quick Exercise

      What does this program do? [ 09t.cpp ]

      More on the type_ids of exceptions [ exceptions.cpp ]

      Input

        The best laid plans Gang Aft Agly

        When in trouble -- raise an exception

        And use the standard exceptions in the C++ Library.

        Standard Exceptions

          <exception>

            class exception{
               public: exception();
                  virtual string what();
            }

          <stdexcept>

          UML inheritance diagram: all have what(). All provide a constructor with a string argument.
           		logic_error("That is not logical, Captain.");

          Put fig 10.1 on your cheat sheet! [ exceptions.dia ] (dia) [ exceptions.png ] (graphic)

        Try....and catch the exceptions later

        Notice the syntax: 'try' intruduces a block of code '{'....'}' in which particular exceptions are handled in a special way. The ways that exceptions are handled are listed at the end of the block with 'catch' functions:
         	try{
      1. block where catches apply
         	}
         	catch ( type of exception )
         	{
      2. what to do with it
         	}
        You can have as many 'catch's as you want. Always list the most specific first.

        Put the catch(...){...} last.

        Things you can do in a catch:

        1. fix the problem some other way
        2. throw something -- let some other part of the program do it.
        3. catch and thro it again: thow;

      3. Always catch standard exceptions using an &!

        Do-It-Yourself Exceptions

        You can throw ANYTHING.

        Better to throw a special kind of exception -- derive your exception from one of the stdeceptions and define a constructor. Page 10 shows how.

        Warning the programmer what can be thrown by a function

        Inspired by the Java "throws" clause.

      Quizzes to come

      Note the material in this class will be tested in a a quiz in class 12.

      Questions

        Do I have to memorize and distinguish all the different C++ exception types

        Not in CSci202. You might be wise to draw the hierarchy on your cheat sheet. You may need the diagram in the lab as well. However, I don't, for example expect you to know how a runtime error differs from a logi error and so on.

        What happens if an exception is raised and is not caught or trapped

        The program aborts with a generic error message. You client probably has a heart attack.

        Please give an example

        Normally we catch errors in a function that calls the function that throws the exception.
         	try {
         		function();
         	} catch (...) { cout << "Catch\n"; }

         void function()
         {
         	...
        .AS_is throw 42;
         	...
         }

        What does a throw actually do

        It undoes any function calls and jumps to the first catch that can handle the object that is thrown.

        How to create my own expction class

         class mine: public standard {
         { public: mine(....) : .... {}
         		...
         };

        Should we remove exceptions when the program is debugged

        No. Wen well done, exceptions become part of the excepted and desirable behavior of your program. Th user never knows what compilcated paths of "try and error" were needed to produce what they wanted.

        How does abort differ from throw

        Abort teminates the program, but you can catch the 'throw' and keep the program running.

        Why do program still crash with all these techniques available

        People still use the old languages.... and some are not well trained in modern techniques.

        Why has my browser started crashing.

        On a Microsoft machine, You've proably got a virus. Get your computer checked out and cleaned... and take more care in the future.

        On a Linux machine -- the code has a bug. Get an update... or join the people who develop open source.

        Exeptions vs assert

        Use assrt as a testing tool. Use exceptions to handle bad data in a function.

        Is there a best way to handle exceptions

        Not really.... there are some good patterns but you have to take time to think up the best thing to do with every one.

        Should we use exceptions in this course

        Yes. In the next Lab!

      . . . . . . . . . ( end of section Questions) <<Contents | End>>

      Quiz

      [ Quiz in 08 ]

    . . . . . . . . . ( end of section CSci202 Computer Science II, Session 09 Exceptions) <<Contents | End>>

    Lab

    [ lab05.html ] (add exceptions to Buffer).
    (Next): [ 10.html ] (input and output revisitted) and Project 3 (Chapter 9 Inheritance) due in.

    Abreviations

  1. TBA::="To Be Announced", something I have to do.
  2. TBD::="To Be Done", something you have to do.
  3. Dia::="A free Open Source Diagramming tool for Linux, Windoze, etc. ".

End