Thread: is such exception handling approach good?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Provided the objects themselves are implemented correctly.

    Of course, there are other things that are part of exception safety. Most importantly, you can never allow your function to be aborted by an exception while any objects are in an inconsistent state. E.g. this copy assignment operator of a string-like class is unsafe:
    Code:
    mystring & mystring::operator =(const mystring &rhs)
    {
      delete[] m_data;
      m_data = new char[rhs.size() + 1];
      std::memcpy(m_data, rhs.m_data, rhs.size() + 1);
      return *this;
    }
    The reason: if the allocation fails, m_data is pointing at invalid memory. The object is in an invalid state.

    (The operator is also unsafe regarding self-assignment, but that's a different issue.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You should also ensure that none of your destructors or swap functions throw an exception: http://www.ubookcase.com/book/Addiso...1lev1sec2.html

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks cpjust,


    Good online book. Money saving. :-)

    Quote Originally Posted by cpjust View Post
    You should also ensure that none of your destructors or swap functions throw an exception: http://www.ubookcase.com/book/Addiso...1lev1sec2.html

    regards,
    George

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Great sample, CornedBee!


    Quote Originally Posted by CornedBee View Post
    Provided the objects themselves are implemented correctly.

    Of course, there are other things that are part of exception safety. Most importantly, you can never allow your function to be aborted by an exception while any objects are in an inconsistent state. E.g. this copy assignment operator of a string-like class is unsafe:
    Code:
    mystring & mystring::operator =(const mystring &rhs)
    {
      delete[] m_data;
      m_data = new char[rhs.size() + 1];
      std::memcpy(m_data, rhs.m_data, rhs.size() + 1);
      return *this;
    }
    The reason: if the allocation fails, m_data is pointing at invalid memory. The object is in an invalid state.

    (The operator is also unsafe regarding self-assignment, but that's a different issue.)

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 11:30 AM
  2. Exception handling
    By Mortissus in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2005, 08:35 PM
  3. Exception handling
    By hariharanpalani in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2005, 05:33 AM
  4. ATL exception handling
    By rzcodeman in forum Windows Programming
    Replies: 1
    Last Post: 06-10-2004, 06:19 PM
  5. Rate My Application: File Exception Handling
    By KingZoolerius66 in forum C++ Programming
    Replies: 5
    Last Post: 12-14-2003, 10:29 AM