1
h13
CS32 S18
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h13: Exceptions

ready? assigned due points
true Tue 05/08 08:00AM Tue 05/15 12:30PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the four lowest scores (if you have zeros, those are the four lowest scores).


Reading: Exceptions, PS 16.1, 16.2

  1. (10 pts) Fill in the information in the header. The following are required to get the 10 "participation" points.
    • Filling in your name and umail address.

    Also: For paper submission PLEASE submit on ONE SHEET OF PAPER, double-sided if at all possible. If you must submit on two printed sheets write name on BOTH sheets and no staples, paperclips, or folded corners.

  2. (5 pts) C++ has an mechanism for throwing exceptions. Many other languages have this feature as well. One interesting way that exception handing varies from one language to another is what type of "thing" can be thrown. Java, for example, has a class called Exception (specifically, java.lang.Exception). The only things you can "throw" in Java are objects of type Exception (or of types that are derived via inheritance from Exception). What is the case for C++ - i.e., what kind of value can be "thrown" in C++?
     
  3. (5 pts) In both C++ and Java, programmers often create special classes for programmer-defined Exceptions. For example, for a project that is doing analysis of word occurrences on Subreddits of the site reddit.com, you might create a NoSuchSubreddit exception for the case where the program is trying to access a Subreddit that does not exist. In Java, if you want to look at the code and find out whether a particular class is being used as an exception, it is easy: you see whether it inherits from java.lang.Exception either directly or indirectly. In C++ what do you look for in the code to indicate whether a particular class is used as an exception?
     
  4. The author of PS makes the point that exception classes can be trivial
    1. (5 pts) Illustrate this point by declaring a complete class specification for a C++ NoSuchSubreddit exception. You should have no difficulty confining your answer to the tiny space given here:
       
    2. (5 pts) Slightly more subtle is this: if the class specification is so trivial, what good it is? Why even declare such a class at all? Be very specific and precise in your answer, but keep it short. (Note: This one actually may require some thought - the answer is not just "lying there" in the book waiting to be found. You will have to really read, digest, and think about the material a bit. Learning may take place. Don't worry - this is a perfectly normal reaction, and any pain you experience will subside.)
       
  5. A standard "Hello World" type example for exception handling is divide by zero.
    1. (5 pts) Write C++ code that creates a (trivial) class for a DivideByZero exception.
       
    2. (5 pts) Using that class, write the prototype for a C++ function named rationalAsDouble that takes two args, int numerator, and int denominator. It coverts these two integers as a fraction into a double. The function should throw a DivideByZero exception if the denominator is zero. Be sure that the function prototype declares that the function may throw the DivideByZero exception (see pp. 911-913 for the syntax).
       
    3. (10 pts) Now write the full function definition for rationalAsDouble that throws the necessary exception if the denominator is zero. Otherwise, it carries out the division and returns the value as a double.