|
I personally never used exceptions for ATL projects. Now a colleague votes strongly for throwing exceptions instead of returning HRESULT s. I did some research, and even here on codeproject I don't find much about ATL and exceptions. To me it appears that exceptions in ATL are not very popular. So I would like to know from you ATL guys:
Do you use exceptions? Or you simply return an HRESULT ?
And most imporant: Why?
Cheers
Imagiro
|
|
|
|
|
I used a combination. A colleague and I developed an exception object with various constructors that could, for example, be thrown with parameters of: a HRESULT, a string describing what failed, the file name it was thrown in, and the line number of the file it was thrown in.
for example:
HRESULT hr = somefunc();
if(FAILED(hr))
{
throw _abc_error(hr, L"Somefunc failed", FILE_AND_LINENO);
}
...
catch(_abc_error& err)
{
MessageBox(err.ErrorMessage(), L"App Name", MB_ICONWARNING | MB_OK);
}
The exception object constructor then took care of writing the error as an event to the System Event log as well as looking up any messages from the HRESULT, or accessing any other error handling mechanism needed.
This resulted in code unclutered with complicated error handling and a standard way of making sure that the log showed not only an error code or message, but a comment on what was being attemted, which .cpp file this was detected in and the line number of the file.
Jonathan
|
|
|
|
|
Personally: I never did like exceptions and avoid them. I did a google on "exceptions C++ pros and cons" and found several discussions. There are some strong opinions on this.
I would much rather test for bad conditions and return an error code as opposed to throwing an exception.
Thanks for your time
PS: This is a nice discussion:
C++ Exceptions: Pros and Cons[^]
|
|
|
|
|
I agree when you say you'd rather return an error code than throw an exception. I found using exceptions locally makes the code clearer and imposes some structure. Having caught the exception locally and dealt with it i.e. usually recorded what exactly went wrong etc I too then prefered to return an error code.
Jonathan
|
|
|
|
|
Having originally started with C, I too prefer returning an error code to be cleaner than catching exceptions, but I'm sure others see exceptions as the true C++ method for handling errors.
|
|
|
|