Anonymouce

Greetings all,

I have recently been tasked to modify a legacy VC++ (MFC) application with poor exception handling mechanism into something more manageable. Towards that end, the least I would like to do is to implement a functionality that can catch any exception - and by that I mean any exception that occurs, so that we can better report the error and take actions to do subsequent tasks such as releasing resources and restarting the application.

Can it be realistically achieved Is it possible in an MFC app to implement a blanket 'catch'



Re: Visual C++ General Catching Error condition

einaros

You can use an ellipsis to catch all C++ exceptions, such as "try {} catch(...) {}", but you won't know what exception class it's an instance of. As far as better reporting goes, you won't really be able to do any. Your options are basically to either
  1. make sure that all exceptions thrown derive from one base class, which at least expose a function to get a message describing the error condition
  2. catch the right exception set of exceptions in the right place
Personally, I'd prefer number two, as that would promote better exception handling in general. Catch-all tends to translate to not actually knowing what may throw, and where. When you don't know what can throw and why, you're also unlikely to know what action you should take in case of an exception being thrown. It's also likely that the exceptions aren't caught caught and dealt with in the right place.

These may not be the words you were looking for, and I agree that it's not always practical to adapt an already large code base. Unfortunately there's no proper way of constructing truly useful catch-all blocks in native C++.





Re: Visual C++ General Catching Error condition

Anonymouce

Thanks einaros:

But where to put a catch block I want to be able to have a function called when an error such as null pointer reference occurs. Is it possible to register one such function





Re: Visual C++ General Catching Error condition

einaros

This has been covered in full, in quite a few threads, on this forum already. Take a look at http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1589651&SiteID=1.

That being said, my humble opinion is that installing custom error handlers is even worse than catch-all blocks. It has the "happy hour, anything can happen!!" ring to it, and I don't like that in software. Predictability first, and if you cannot make reliable predictions, isolate it or avoid it completely.





Re: Visual C++ General Catching Error condition

Sdi

Override CWinApp::Run() and put your try{} around the call to CWinThread->Run().





Re: Visual C++ General Catching Error condition

einaros

Sdi wrote:
Override CWinApp::Run() and put your try{} around the call to CWinThread->Run().


That won't catch structured exceptions, though. IMHO, leaving all exception handling out of it, and merely overriding the error handlers would be best. You can make all hard errors soft; not by continuing execution, but logging where it happened and what the stack looked like (a minidump, if you will). It'd also leave the code free of catch-all blocks, which I consider evil (see my previous post).





Re: Visual C++ General Catching Error condition

Anonymouce

Thanks sidi and einaros,
The code is rather complex, from 1995 with traces of both MFC and Win32. It also has SEH at places. On top, it has some 200 classes which makes it a rather large project.
sidi: I tried overriding Run and it seems to work fine. I'll certainly implement it. A couple of tries gave good results. As for existing SEH, I guess I'll modify (200+ catch blocks) to log the errors into some kind of MSMQ so they can be retrieved later.

My next question is :
Now that I have resolved to make changes to 200+ places ( mainly catch blocks), how to gather as much data as possible, such as variables, stack etc

PS: I will most possibly ask this question in a seperate thread. But pls do not ignore to reply. A short pointer would be helpful. Thanks you all in advance.




Re: Visual C++ General Catching Error condition

einaros

__try
{ ... }
__except(EXCEPTION_EXECUTE_HANDLER)
{
// log error and exit application
}

Within the except block, information about the caught structured exception can be obtained through GetExceptionInformation and GetExceptionCode.





Re: Visual C++ General Catching Error condition

Sdi

I agree; I'd be perfectly happy to see "catch(...)" removed from the language.