Re: cannot catch exception

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I changed the code to be:

if (!pObj)
{
continue;
}

try
{
//if pObj is some garbage
//the next line will blow up
pObj->GetPosition();
}
catch(...)
{
//let us skip invalid pObj
AfxMessageBox("got exception");
continue;
}

The first test is passed because there is something in the variable (but not
a valid object).

The pObj is taken from a collection. Something like
CDrawObj* pObj = m_objects.GetNext(pos);

And I changed calling method IsValid() to method GetPosition() which returns
coordinates of the object.

The exception is still not caught.

Vaclav


"Josh McFarlane" <darsant@xxxxxxxxx> píse v diskusním príspevku
news:1133301975.992985.102180@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Vaclav wrote:
>> if (!pObj->IsValid())
>> {
>> continue;
>> }
>> The problem disappears when I turn optimization off. The question is why
>> the
>> handler is not called?
>
> The compiler is looking at IsValid, seeing that it always returns true,
> and skipping the entire thing all together.
>
> Two more questions:
> Is the pointer initialized to NULL?
> If so, you can do the following to check to see if a value has been
> passed:
>
> if (pObj)
> {
> //Valid pointer
> //Do stuff
> }
> else
> {
> //Invalid pointer
> //Complain
> }
>
> If the previous answer was yes, and you still have invalid pointers,
> how is that occuring?
>


.



Relevant Pages

  • Re: map::clear()
    ... where id is a short and pObj is a allocated pointer to a myObj. ... > map.clear() deletes the elements in the map. ...
    (microsoft.public.vc.stl)
  • Re: I cannot see the need for auto_ptr?!
    ... but returning a pointer from foo is a bad progamming practice. ... void bar(int Type) ... Obj* pObj; ... free yourself from human errors and to easen maintenance (who says that this test ...
    (comp.lang.cpp)
  • Re: Datatype missalignment exception - windows mobile 2003
    ... WINDOWPTR structure. ... Your "w" pointer is misaligned, ... or "pObj" field of WINDOWPTR is misaligned. ... pWinWid = win_GetWidgetCC; ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Pointers
    ... A pointer is just an address of something in memory - one says it "points ... // pObj now points where it pointed before, ... The delete line will tidy up ... unused memory space and the =NULL line will tidy up your code. ...
    (microsoft.public.vc.mfc)
  • Re: cannot catch exception
    ... //let us skip invalid pObj ... because it blows up when function IsValid() gets called. ... > Vaclav wrote: ... >> exception, ...
    (microsoft.public.vc.mfc)