[Webinar] Streamline your web hosting managementRegister Today


Catching exceptions in MFC

Posted on 1999-11-30
Medium Priority
Last Modified: 2012-06-22
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.
Question by:SDU
  • 2

Accepted Solution

KangaRoo earned 300 total points
ID: 2244166
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.
LVL 22

Expert Comment

ID: 2244464
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.

Author Comment

ID: 2244465
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

Author Comment

ID: 2244487
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.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question