Click on the banner to return to the class reference home page.

exception


Standard Exception

Summary

Classes supporting logic and runtime errors.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

 #include <exception>

 class exception;

Description

The class exception defines the base class for the types of objects thrown as exceptions by Standard C++ Library components, and certain expressions, to report errors detected during program execution. Users can also use these exceptions to report errors in their own programs.

Interface

class exception {

   public:
     exception () throw();
     exception (const exception&) throw();
     exception& operator= (const exception&) throw();
     virtual ~exception () throw();
     virtual const char* what () const throw();
 };

 class logic_error : public exception {
   public:
     logic_error (const string& what_arg);
 };

 class domain_error : public logic_error {
   public:
     domain_error (const string& what_arg);
 };

 class invalid_argument : public logic_error {
   public:
     invalid_argument (const string& what_arg);
 };

 class length_error : public logic_error {
   public:
     length_error (const string& what_arg);
 };

 class out_of_range : public logic_error {
   public:
     out_of_range (const string& what_arg);
 };



 class runtime_error : public exception {
   public:
     runtime_error (const string& what_arg);
 };

 class range_error : public runtime_error {
   public:
     range_error (const string& what_arg);
 };

 class overflow_error : public runtime_error {
   public:
     overflow_error (const string& what_arg);
 };

 class underflow_error : public runtime_error {
   public:
     underflow_error (const string& what_arg);
 };

Constructors

exception() 
throws();
exception(const exception&) 
throws();

Destructor

virtual 
~exception()
throws();

Operators

exception& 
operator=(const exception&) 
throws();

Member Function

virtual const char* 
what()const 
throws();

Constructors for Derived Classes

logic_error::logic_error(const string& what_arg);
domain_error::domain_error(const string& what_arg);
invalid_argument::invalid_argument(const string& what_arg);
length_error::length_error(const string& what_arg);
out_of_range::out_of_range(const string& what_arg);
runtime_error::runtime_error(const string& what_arg);
range_error::range_error(const string& what_arg);
overflow_error::overflow_error(const string& what_arg);
underflow_error::underflow_error(
                    const string& what_arg);

Example

  //
  // exception.cpp
  //
  #include <iostream.h>
  #include <stdexcept>

  static void f() { throw runtime_error("a runtime error"); }

  int main ()
  {
     //
     // By wrapping the body of main in a try-catch block 
     // we can be assured that we'll catch all exceptions 
     // in the exception hierarchy.  You can simply catch 
     // exception as is done below, or you can catch each 
     // of the exceptions in which you have an interest.
     //
     try
     {
         f();
     }
     catch (const exception& e)
     {
         cout << "Got an exception: " << e.what() << endl;
     }
     return 0;
  }

©Copyright 1996, Rogue Wave Software, Inc.