Re: ? Handling assertions

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Alec S. (a_at_a.com)
Date: 08/17/04


Date: Tue, 17 Aug 2004 13:17:43 -0400

I know it is settings up something invalid. That's why I want to handle it.
I need to allow the user to manually input the handle for a window or the
CWnd pointer. Here's a stripped down example of the code:

    ...
    HWND handle=m_HANDLE;
    CWnd* wnd=FromHandle(handle);
    try
    {
        CString t;
        wnd->GetWindowText(t);
    }
    catch (...) {
        wnd=NULL;
        AfxMessageBox("That handle is invalid.");
    }
    ...

This does not work because it gives me an assertion. However, using similar
code works just fine:

    ...
    CWnd* wnd=(CWnd*)m_POINTER;
    try
    {
        CString t;
        wnd->GetWindowText(t);
    }
    catch (...) {
        wnd=NULL;
        AfxMessageBox("That handle is invalid.");
    }
    ...

    The reason that the second one works is because an invalid window
pointer causes an exception. The first one is a little different. If I
input an invalid handle, I still manage to get a valid CWnd*, but using
GetWindowText on it causes an assertion. Therefore there is no exception
and my validity check does not work.

    What can I do to get around this? Is there a better way to test if a
CWnd*/HWND is valid?

--
Alec S.
alec <@> synetech <.> cjb <.> net
"Jonathan Wood" <jwood@softcircuits.com> wrote in message
news:eqa1gYBhEHA.592@TK2MSFTNGP11.phx.gbl...
> You are not supposed to handle assertions other than by reworking your
code.
> In effect, assertions identify bad programming. You provided very little
> info about the line that causes the assertion. You should be able to use
> Retry to step through the code. Either way, your code is setting up
> something invalid somehow and you need to figure out what is happening so
> you can rework it.
>
> -- 
> Jonathan Wood
> SoftCircuits
> http://www.softcircuits.com
> Available for consulting: http://www.softcircuits.com/jwood/resume.htm
>


Relevant Pages

  • Re: ? Handling assertions
    ... Alec S. wrote: ... > I know it is settings up something invalid. ... > This does not work because it gives me an assertion. ...
    (microsoft.public.vc.mfc)
  • Re: An outstanding question.
    ... an _invalid_ assertion. ... that I didn't such assertion is true, ... Do you maintain that the assertion that PA is consistent ... The reason why I chose it is that it can do model theory and so serves ...
    (sci.logic)
  • Re: An outstanding question.
    ... an _invalid_ assertion. ... that I didn't such assertion is true, ... Do you maintain that the assertion that PA is consistent ... The best we could do is to try intuiting a model of T; ...
    (sci.logic)
  • Re: An outstanding question.
    ... either cGC or ~cGC true (as long as the concept of the natural numbers ... an _invalid_ assertion. ... that I didn't such assertion is true, ... Do you maintain that the assertion that PA is consistent ...
    (sci.logic)
  • Re: An outstanding question.
    ... either cGC or ~cGC true (as long as the concept of the natural numbers ... an _invalid_ assertion. ... that I didn't such assertion is true, ... Do you maintain that the assertion that PA is consistent ...
    (sci.logic)