Thread: Exceptions

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    1

    Exceptions

    Now suppose I have a pointer to an object and this object is deleted by another thread or earlier in the program inadvertently. Not really important why.

    What kinda of exception is raised if the pointer points to a deleted object and a method is called on this object.

    class CSample
    {
    public:
    void DoSomthing()
    { . . . . . }
    };

    void main()
    {
    CSample * sample = new CSample;
    . . . . .
    delete sample;
    . . . . .
    try
    {
    sample->DoSomething();
    }
    catch(...)
    {
    //handle invalid reference
    }
    . . . .
    }

    while this will work. I would rather only capture the fact that this pointer is no longer valid. So what should go in in place of the ...?

  2. #2
    Not sure what type of exception is thrown but I know this is considered illegal. They made the auto_ptr<>, I think that's how they spell it, for this reason.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    OneStiffRod is right......the language doesnt promise an exception in this situation...it only says that the result is "undefined" which means it could work, could compute the answer to the meaning of life or it could crash (usually the latter). Dont do it.

    Also, drop the void main...it should be int main

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  2. Exceptions and constructors
    By Mostly Harmless in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2007, 11:20 AM
  3. Long-lasting objects that throw exceptions
    By drrngrvy in forum C++ Programming
    Replies: 7
    Last Post: 10-05-2006, 04:30 PM
  4. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 01:49 PM
  5. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 09:21 AM