MFC Exception Handling - What happens to an uncaught exception thrown in an event-handler?

Posted on 2007-10-07
Last Modified: 2013-12-04
If an event handler throws an exception, and it is not caught by a programmer-defined try/catch block, is the event canceled?

In all cases, only in some?

Question by:oxygen_728
    LVL 43

    Expert Comment

    I think an unhandled exception will always result in the program crashing, not the event being canceled.

    Expert Comment

    If the unhandled exception occurs in the event handler, the application crashes and the message won't be removed from the message queue.
    LVL 43

    Expert Comment

    sjith2000 - please read the comments by other experts first.

    Expert Comment

    Hi AndyAinscow,
      I am very glad to say that I had already seen the comment written by you.I also feel that the application will crash as you think.Also I feel that the message won't be removed from the message queue

    Author Comment

    Thanks for the comments thus far,

    I have had my suspicions that the application would crash.

    I hope to test it soon.

    Thanks for the input
    LVL 43

    Expert Comment

    To duplicate a previous experts comment with referring to it is not polite (or useful) behaviour.
    If you agree with a comment posted earler then why not say so.
    Andy is correct, the app will crash.
    LVL 43

    Accepted Solution

    Give this a quick try.

    add the folllowing class to a header file

    class CTest {
       CTest() {};
       ~CTest() {};
       const char *ShowReason() const {
          return "Exception in CTest class.";

    and to a button handler in an MFC based dialog app.

    void CxyzDlg::OnBnClickedButton1()
          throw CTest();

    build in release mode and click on the button, watch the app crash.
    LVL 39

    Expert Comment

    >>>> I feel that the message won't be removed from the message queue
    an exception is independent of the existence or non-existance of message queues. Exeption handling is realized by some kind of 'long jumps' along the call stack.

    >>>> have had my suspicions that the application would crash.
    What is the reason for your doubts. As Andy showed, it is simple to verify that unhandled exceptions lead to a crash.

    Regards, Alex

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now