2

I'm reading chapter 16 so that I may begin my assignments for my C++ class. This section is on exception handling. I understand the concept behind a try / catch construct, however, one of the examples in the book is a bit confusing to me. I'm hoping for some explanation as to how this is working. The sample code is below:

// Includes, header guards, and namespace std...
class IntRange
{
 private:
  int intput;
  int lower;
  int upper;

 public: 
  // Exception class
  class OutOfRange { }; // This is exactly how it appears in the text.

  IntRange(int low, int high) { lower = low; upper = high; }
  int GetInput()
  {
    cin >> input;
    if (input < lower || input > upper)
      throw OutOfRange(); // <-- This is my question in particular. What is this?
    return input;
  }
};

// End header guard.

// Program entry point.

int main()
{
  IntRange range(5, 10)
  int userValue;

  cout << "Enter a value in the range 5 - 10: ";
  try
  {
    userValue = range.getInput();
    cout << "You entered " << userValue << endl;
  }
  catch (IntRange::OutOfRange) // <-- Again, what is this delcaration and how can
                               // this data type be defined when IntRange does not
                               // have a default constructor?
  {
    cout << "That value is out of range.\n";
  }

  return 0;
}

The code is exactly as it appears in the textbook, except I put some stuff on the same line in order to keep the question from becomming really long.

If you notice any errors, it's most likely a typo, but the most important points have been double-checked.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179

4 Answers4

2
 throw OutOfRange();

This creates a new instance of class OutOfRange and throws it. Remember that you can also create an instance like this:

my_method( MyClass() );

instead of:

MyClass obj; 
my_method( obj );

Now:

catch (IntRange::OutOfRange)

OutOfRange is an inner or nested class of IntRange. See here

ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
  • 2
    I just read that example on IBM's site. Thanks! Our book introduces new ideas and fails to provide any explanation. (= –  Apr 15 '12 at 15:37
  • see [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for some better books. – ApprenticeHacker Apr 15 '12 at 15:38
1
  throw OutOfRange(); // <-- This is my question in particular. What is this?

This default constructs an object of OutOfRange then throws it.
Even if you do not define a methods in a class the compiler will automatically generate a couple for you one of these is the default constructor. Thus even if you don't specify a constructor there will be one there (Do a google on rule 3/5 for explanation of what methods are generated by the compiler).

catch (IntRange::OutOfRange) // <-- Again, what is this delcaration and how can
                             // this data type be defined when IntRange does not
                             // have a default constructor?

Here we are catching an object of type IntRange::OutOfRange. Note: we are not catching an object of IntRange. We are catching the class OutOfRange that happens to be defined inside the class IntRange (apart from where it is defined there is no other relationship).

Also note: Unless you disable it the compiler will automatically generate a copy constructor for all classes. Thus exceptions are usually copy constructed from the throw point to the catch point (its a tiny bit more complex). So your exception object must be copyable.

Also note it is best to catch exceptions by const reference:

catch (IntRange::OutOfRange const& e)

The avoids problems associated with slicing objects that are part of exception hierarchies.

Martin York
  • 257,169
  • 86
  • 333
  • 562
  • OK, I see. To me it seemed like an object of OutOfRange could not be instantiated without first having an instance of an IntRange object. So if I am understanding you, it would be perfectly legal to delecare an object as such: IntRange::OutOfRange oor; And if it had members, I could interact with them without having ever created an object of IntRange? –  Apr 15 '12 at 15:41
  • @fhaddad78: Correct. There is no explicit relationship between object of these types. OutOfRange is a complete type in its own right and has no implicit references too IntRange – Martin York Apr 15 '12 at 15:47
0

They are creating a custom exception (as a class) called OutOfRange and throwing it.

This way, you can specifically catch an OutOfRange exception, this way:

try
{
    //do something
}
catch (OutOfRange o)
{
    //you know the input was out of range
}
catch (Exception e)
{
    //something else went wrong
}
Ayush
  • 41,754
  • 51
  • 164
  • 239
0

your OutOfRange class belongs to the IntRange class as a whole - it doesn't belong to any particular object of that class.

In order to use OutOfRange there's no requirement even for IntRange to actually be usable as a concrete class , since IntRange::OutOfRange simply specifies the fully-qualified typename and not an object.

in C++, type names are only important at compile time; data type information (along with variable names and all sorts of other stuff like that in your code) is generally stripped out in its entirety by the compiler - data types only exist to help you write code and to do your debugging.

Ben Cottrell
  • 5,741
  • 1
  • 27
  • 34
  • Thanks. Your explanation as well as a couple others explained what is going on very clearly. –  Apr 15 '12 at 15:45