Exception Handling

One strength of managed code lies in the structured approach to error handling using exceptions. Recall our native code fragment in the context of the goto statement’s discussion in Chapter 7, “Simple Control Flow,” in the section “The goto Statement?” Let’s refresh our minds and highlight a few aspects of it:

// Dealing with HRESULT return codes in C or C++.HRESULT InitDownloadManager(){    IBackgroundCopyManager* g_pbcm = NULL;    HRESULT hr;    hr = CoInitializeEx(NULL,                        COINIT_APARTMENTTHREADED);    if (FAILED(hr))        goto Exit;    hr = CoCreateInstance(__uuidof(BackgroundCopyManager), NULL,                          CLSCTX_LOCAL_SERVER,                          __uuidof(IBackgroundCopyManager), ...

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.