When I'm programming C code for Windows, should I "default" to using SEH's __try
...__finally
blocks, or is it considered bad practice to do so unnecessarily?
In other words, which one below (for example) is considered better practice, and why?
HDC hDCCompat = CreateCompatibleDC(hDC);
__try
{
HBITMAP hBmpCompat = CreateCompatibleBitmap(hDC, ...);
__try
{
SelectObject(hDCCompat, hBmpCompat);
BitBlt(hDC, ..., hDCCompat, ...);
}
__finally { DeleteObject(hBmpCompat); }
}
__finally { DeleteObject(hDCCompat); }
versus
HDC hDCCompat = CreateCompatibleDC(hDC);
HBITMAP hBmpCompat = CreateCompatibleBitmap(hDC, ...);
SelectObject(hDCCompat, hBmpCompat);
BitBlt(hDC, ..., hDCCompat, ...);
DeleteObject(hBmpCompat);
DeleteObject(hDCCompat);
Clarification
I forgot to mention:
My idea behind this was that, if someone inserts more code later into the block (e.g. an early return from the function), my code would still perform the cleanup, rather than exiting prematurely. So it was supposed to be more preventive than anything else. Should I still avoid using SEH anyway?