global exception handlers in MFC
Posted: Sun Sep 14, 2003 3:57 pm
Is it possible to set up a global exception frame in a VC++ program built using the Application Wizard? In a conventional C++ program, I would normally put a [tt]try[/tt]/[tt]catch[/tt] frame at the top level of the [tt]main()[/tt] function, like so:
In a program written using only the WIN32 API calls, I expect one would do the same in [tt]WinMain()[/tt]. However, AFAICT, there is no equivalent top level method for the [tt]CxxxApp[/tt] class generated by the App Wizard; neither the class constructor, nor [tt]InitApplication()[/tt], nor [tt]InitInstance()[/tt] seem to be where it should go. Is there anywhere else I could try - the program's [tt]CMainFrame[/tt] class, for example? Is it even possible to set up a global exception handler in MFC, without resorting to [tt]set_unexpected()[/tt] and [tt]set_terminate()[/tt]? The books I've seen on VC++ and MFC all seem to give little if any coverage to exception handling; usually all they do is reiterate the basic C++ mechanism, without addressing how it would apply to an MFC application.
Also, is there any advantage to using the [tt]TRY[/tt]/[tt]CATCH[/tt] macros instead of the built-in [tt]try[/tt]/[tt]catch[/tt] expressions?
Finally, is there any reasonable way to compile an AppWiz-generated program using another compiler (specifically, MingW gcc 3.2 run from Dev-C++) without modifying the code to such an extent that it would no longer work with VC++? This is not a critical task, but I prefer to check programs with another development environment whenever possible, as a way of highlighting problems which the original compiler might have missed.
Please bear with me if I seem to have any obvious misconceptions about MFC and AFX; I have, up until now, largely avoided them, as they seemed more complicated than they were worth (even compared to the bare Win32 API). When I first read about it years ago, my impression was that it is an excellent example of how not to design a GUI object API, and I have yet to see anything that would convince me otherwise (though in all fairness, I have yet to see what I would consider to be a good GUI API for C++ under any system).
Since I will doubtless have to do more C++ MFC programming in the future, I would like to finally learn it, but so far it has been quite aggravating (I've been told that after working with it for a coule of months it will make perfect sense, but I'm not sure I believe it). Any elucidation or advice (including web pointers and book recommendations) would be helpful.
Code: Select all
int main(int argc, char* argv[])
{
try
{
// program code goes here
}
catch ( ... )
{
// default exception handler goes here
}
}
Also, is there any advantage to using the [tt]TRY[/tt]/[tt]CATCH[/tt] macros instead of the built-in [tt]try[/tt]/[tt]catch[/tt] expressions?
Finally, is there any reasonable way to compile an AppWiz-generated program using another compiler (specifically, MingW gcc 3.2 run from Dev-C++) without modifying the code to such an extent that it would no longer work with VC++? This is not a critical task, but I prefer to check programs with another development environment whenever possible, as a way of highlighting problems which the original compiler might have missed.
Please bear with me if I seem to have any obvious misconceptions about MFC and AFX; I have, up until now, largely avoided them, as they seemed more complicated than they were worth (even compared to the bare Win32 API). When I first read about it years ago, my impression was that it is an excellent example of how not to design a GUI object API, and I have yet to see anything that would convince me otherwise (though in all fairness, I have yet to see what I would consider to be a good GUI API for C++ under any system).
Since I will doubtless have to do more C++ MFC programming in the future, I would like to finally learn it, but so far it has been quite aggravating (I've been told that after working with it for a coule of months it will make perfect sense, but I'm not sure I believe it). Any elucidation or advice (including web pointers and book recommendations) would be helpful.