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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.