Catching exceptions in MFC

I'm using VC 6 with MFC

We have implemented a design by contract technique in our code and
it's working well.
The technique is simple, we simply have to put a condition that will verify if
the "contract" is respected. If not, we simply throw an exception.
At some strategic places, we have try-catchs in wich we can display
to user the cause of the exception.
Those exception are like the ASSERTs we can see in MFC Code.
I think that MFC removes those ASSERTs in release mode.
The problem I'm facing is that those strategeic "try-catch" are easy
to place in our generic classes but I don't know where to place them when
we are using the MFC framework.

Here is an example:

I have a dialog in which I placed intentionally a pre-condition that is always false
(for the purpose of this example only)

BOOL MyDialog::OnInitDialog()


   return TRUE;  // return TRUE unless you set the focus to a control
                 // EXCEPTION: OCX Property Pages should return FALSE

Think of the PRE_COND macro as an ASSERT that throws a user exception derived
from CException if the contract that it contains is not respected (is false).

My problem is that my exception is caugth in MFC

LRESULT AFXAPI AfxCallWndProc(CWnd* pWnd, HWND hWnd, UINT nMsg,
      WPARAM wParam = 0, LPARAM lParam = 0)
      // Catch exceptions thrown outside the scope of a callback
      // in debug builds and warn the user.
      LRESULT lResult;
      (...)    // It is caugth right here
               // and it simply display "internal error" on screen


Is there something I can do to intercept that process before MFC does
without having to place a try catch in every method of every interface classes ?

Thanks for the help.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

We had something similar with OWL. Solution: display the message before the actual throw. You are using macro's already, so should not be hard.
I have to go for today. If any Q, ask.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The places that MFC catches exceptions (and does no rethrow them) are the places where it is logical to do so, places where it makes no sense to continue throwing the exception.  for example, it makes no sense to throw an exception from a callback procedure.  The callback procedure is called by windows.  It is not necessarily a C++ procedure so it can't necessarily support C++ exceptions.  Even if it could, it cetainly would not be able to take any reasonable action in response to the exception.  Consider for exampe, if you a program sends a message to one of you progran's windows.  if the message handler, a callback function, throws an exception, Where should that exception propegate to?  To the other program?  That program certainly has no way of handling the exception so it would have to be terminated.  That seems a little dangerious!

The question really is what behavior do you want to have when a exception is detected in this sort of event?  Do you want the program to end?  Then you should catch the exception inside the window procedure and abort the program before the exception is returned to MFC and the OS.
SDUAuthor Commented:
It's not exactly what I was thinking to get at first but it will answer my needs.
I just modified a little your solution by overloading the "ReportError" method that can be found in CException and that is called by MFC's framework at the place where it catches my exception.

It does about the same as your solution so you deserve your points.

Thanks for your point of view
SDUAuthor Commented:
I see that nietod added a comment before I do, my last comment was in response to Kangoroo.

For nietod, as I said, I found a good solution only to display the error (with ReportError) and that's it. I was not sure at first but that will do the job.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.