Exception Handling

Error handling is a tricky business. Disciplined error code checking on function return has plagued developers for many years. For example, the following fragment is an excerpt from the Platform SDK documentation on a COM-based API for file transfer:

IBackgroundCopyManager* g_pbcm = NULL;HRESULT hr;// Specify the appropriate COM threading model for your application.hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);if (SUCCEEDED(hr)){    hr = CoCreateInstance(__uuidof(BackgroundCopyManager), NULL,                          CLSCTX_LOCAL_SERVER,                          __uuidof(IBackgroundCopyManager),                          (void**) &g_pbcm);    if (SUCCEEDED(hr))    {        // Use g_pbcm to ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.