Re: try/catch in OnDraw function
On Sun, 02 Sep 2007 21:38:28 -0700, Kuenga <sagkumar@gmail.com> wrote:
Hi,
Is it a good idea to put try/catch block in ondraw function of a view
and when an exception arises
close that frame containing the view in the catch block.
No, that would be a pretty bad idea for a couple of reasons.
1. Inside a paint handler, you don't want to do anything but paint. You
don't want to display message boxes, manipulate windows, etc. If you must
do something extraordinary, use PostMessage. Most code ignores exceptions
and relies on MFC's top-level handler to catch them and report errors.
Offhand, inside paint handlers, I don't know if MFC suppresses the message
box exception reporting it normally does, but given the apparent rarity of
exceptions inside paint handlers, it doesn't seem to matter much that most
code may potentially violate the rule about showing message boxes during a
paint. The problem is, you can get caught in an endless loop of paint
failure, messagebox, paint failure, message box, and that's a real pain for
the user.
2. Painting failures are rare, and it's a lot more likely that nothing will
be painted than the wrong information will be painted. I can't imagine any
exception warranting the drastic action of closing the window. If you're
really worried about displaying incorrect information, it would be better
to catch the exception, erase the window using pure Windows API, and print
a short message to the window.
--
Doug Harrison
Visual C++ MVP