C++ Tutorial/Exceptions/Exception

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Declaring exception specifications in class hierarchy

<source lang="cpp">#include <string>

  1. include <typeinfo>

class base { public:

 virtual void f() throw();
 virtual void g(); // can throw anything
 virtual void h() throw(std::string);

}; class derived : public base { public:

 virtual void f() throw();    // OK: same as base
 virtual void g() throw(int); // OK: subset of base
 //virtual void h() throw(int); // Error: int not in base

}; class more : public derived { public:

 //virtual void f();            // Error: can throw anything
 virtual void g() throw();    // OK

};</source>

Demonstrating stack unwinding

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <stdexcept>

using std::runtime_error; void f3() throw ( runtime_error ) {

  cout << "In f 3" << endl;
  throw runtime_error( "runtime_error in f3" ); 

} void f2() throw ( runtime_error ) {

  cout << "f3 is called inside f2" << endl;
  f3(); 

} void f1() throw ( runtime_error ) {

  cout << "f2 is called inside f1" << endl;
  f2();

} int main() {

  try 
  {
     cout << "f1 is called inside main" << endl;
     f1();
  }
  catch ( runtime_error &error )
  {
     cout << "Exception occurred: " << error.what() << endl;
     cout << "Exception handled in main" << endl;
  }
  return 0;

}</source>

f1 is called inside main
f2 is called inside f1
f3 is called inside f2
In f 3
Exception occurred: runtime_error in f3
Exception handled in main

Handling Derived-Class Exceptions

<source lang="cpp">#include <iostream> using namespace std;

class B { };

class D: public B { };

int main() {

 D derived;
  
 try {
   throw derived;
 }
 catch(B b) {
   cout << "Caught a base class.\n";
 }
 catch(D d) {
   cout << "This won"t execute.\n";
 }
  
 return 0;

}</source>