Exception question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • junw2000@gmail.com

    Exception question

    class A
    {
    public:
    A() { p_a = new int;}
    ~A() { delete p_a; } //Line1
    private:
    int *p_a;
    }

    void f()
    {
    try
    {
    A a1;
    cout << "In f()" << endl;
    g(); //Line2
    A a2;
    }
    catch()
    {
    cout << "In catch" << endl;
    }
    }

    void g()
    {
    cout << "In g()" << endl;
    throw; //Line3
    }

    When an exception is thrown at Line3 in g(), stack will unwind at
    Line2 in f(). When a1 is destructed, will Line1 be excecuted so that
    p_a is deleted? How about a2?

    Thanks.






    }
  • Paavo Helde

    #2
    Re: Exception question

    junw2000@gmail. com kirjutas:
    class A
    {
    public:
    A() { p_a = new int;}
    ~A() { delete p_a; } //Line1
    private:
    int *p_a;
    }
    >
    missing ;

    void f()
    {
    try
    {
    A a1;
    cout << "In f()" << endl;
    g(); //Line2
    A a2;
    }
    catch()
    You probably meant "catch(...) " here.
    {
    cout << "In catch" << endl;
    }
    }
    >
    void g()
    {
    cout << "In g()" << endl;
    throw; //Line3
    You probably meant "throw 42;" here, throw without an argument can be
    used only from inside a catch handler.
    }
    >
    When an exception is thrown at Line3 in g(), stack will unwind at
    Line2 in f(). When a1 is destructed, will Line1 be excecuted so that
    p_a is deleted? How about a2?
    >
    Yes, the language is designed so that it would be possible to write
    exception-safe code. a1 is destructed properly and a2 never gets created.

    hth
    Paavo

    Comment

    • Salt_Peter

      #3
      Re: Exception question

      On Aug 6, 1:52 am, junw2...@gmail. com wrote:
      class A
      {
      public:
      A() { p_a = new int;}
      ~A() { delete p_a; } //Line1
      private:
      int *p_a;
      >
      }
      >
      void f()
      {
      try
      {
      A a1;
      cout << "In f()" << endl;
      g(); //Line2
      A a2;
      }
      catch()
      {
      cout << "In catch" << endl;
      }
      >
      }
      >
      void g()
      {
      cout << "In g()" << endl;
      throw; //Line3
      >
      }
      >
      When an exception is thrown at Line3 in g(), stack will unwind at
      Line2 in f(). When a1 is destructed, will Line1 be excecuted so that
      p_a is deleted? How about a2?
      >
      Thanks.
      >
      }
      When in doubt, prove it:

      #include <iostream>

      class A
      {
      int* p_a;
      public:
      A() : p_a(new int) { std::cout << "A()\n"; }
      ~A()
      {
      delete p_a;
      std::cout << "~A()\n";
      }
      };

      void g()
      {
      std::cout << "In g()" << std::endl;
      throw 1; //Line3
      }

      void f()
      {
      try
      {
      A a1;
      std::cout << "In f()" << std::endl;
      g(); //Line2
      A a2;
      }
      catch(...)
      {
      std::cout << "In catch" << std::endl;
      }
      }

      int main()
      {
      f();
      }

      /*
      A()
      In f()
      In g()
      ~A()
      In catch
      */

      Comment

      Working...