Thread: exception classes

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595

    exception classes

    Prelude recently posted some code (reproduced below) demonstrating the apparent use of an exception class called invalid_argument that appears to be included in namespace std and has at least one method, called what(). If this a correct interpretation of the use of invalid_argument then I would like to learn more about it and the exception classes in namespace std in general. Can someone tell me more about exception classes in namespace? For example---where do I look for them so I can learn what they are and what methods are available for each, etc.? Until now I thought all exception classes were user defined and not part of namespace std like cout/cin/cerr etc.

    Code:
    #include <stdexcept>
    #include <iostream>
    #include <sstream>
    #include <string>
    
    template <typename T>
    T string_cast ( std::string& s )
    {
      T value;
    
      std::istringstream sin ( s );
      sin>> value;
    
      if ( !sin )
        throw std::invalid_argument ( "Value must be an integer" );
    
      return value;
    }
    
    int main()
    {
      int a;
      std::string s = "12345";
    
      try {
        a = string_cast<int> ( s );
    
        std::cout<< a <<std::endl;
      }
      catch ( std::invalid_argument& ia ) {
        std::cerr<< ia.what();
      }
    
      std::cin.get();
    }

    I also have a question on the syntax of this line:


    throw std::invalid_argument ( "Value must be an integer" );

    as it appears to be throwing a class name rather than an instance of a class, or is it declaring an exception class in namespace standard with the string as the argument to be passed to the screen when successfully thrown/caught---but then why bother with the what() method below:

    catch ( std::invalid_argument& ia ) {std::cerr<< ia.what();}

    where ia appears to be a reference to an instance of the invalid_argument class, said class having a method called what() that will display the string used as the argument above.

    Thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    C++ offers an exception class hierarchy that covers a broad range of generic exceptions:
    Code:
    exception
      logic_error
        length_error
        domain_error
        out_of_range
        invalid_argument
      runtime_error
        range_error
        overflow_error
        underflow_error
      bad_alloc
      bad_exception
      bad_cast
      bad_typeid
      ios_base::failure
    All of those classes are derived from exception, which looks something like this:
    Code:
    class exception
    {
    public:
      exception() throw();
      exception ( const exception& ) throw();
      exception& operator= ( const exception& ) throw();
      virtual ~exception() throw();
    
      virtual const char *what() const throw();
    private:
      // ...
    };
    >throw std::invalid_argument ( "Value must be an integer" );
    invalid_argument is defined like so:
    Code:
    namespace std {
      class invalid_argument : public logic_error {
      public:
        explicit invalid_argument(const string& what_arg);
      };
    }
    The throw statement calls the constructor for class invalid_argument with a string that can be printed in the handler describing the exact problem. Most of the classes described above can be thrown the same way, they are all very similar.

    >catch ( std::invalid_argument& ia ) {std::cerr<< ia.what();}
    The catch statement knows that an invalid_argument was caught, but it doesn't know where or what happened. The string passed to the constructor when invalid_argument was thrown describes exactly what went wrong according to the programmer, and the what() member function derived from class exception prints that string as a diagnostic message telling the reader exactly what the programmer thought happened.

    -Prelude
    My best code is written with the delete key.

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    A good advice: When you throw an exception, always throw a class derived from std::exception. That makes catching much easier.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    enlightening responses, both. Thank you.

    Are the responses something you have developed based on experience or do you have a reference I could review? My current references that review exceptions aren't nearly as helpful as your responses, but I suspect there are additional details (that can't be included in a restricted setting such as this) to be learned before mastering usage of exceptions.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    My response is based on the fact that it was very inconvenient having to catch exceptions from C++ and myself in two different catch() clauses.

    Scott Meyers book "More Effective C++" covers exceptions; this is from the ToC:

    Item 9: Use destructors to prevent resource leaks
    Item 10: Prevent resource leaks in constructors
    Item 11: Prevent exceptions from leaving destructors
    Item 12: Understand how throwing an exception differs from passing a parameter or calling a virtual function
    Item 13: Catch exceptions by reference
    Item 14: Use exception specifications judiciously
    Item 15: Understand the costs of exception handling

    IMO, that book is good for programmers that have ceased being newbies.
    Last edited by Sang-drax; 11-01-2002 at 12:34 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Thanks, I'll find/look at a copy of that book.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  3. Handle C++ exception and structured exception together
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2008, 09:21 PM
  4. Exception handling framework based on multiple inheritance
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2007, 10:17 AM
  5. exception handling classes in IRIX....?
    By BrianK in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2003, 03:02 PM