liveBook · Manning

Try-catch statement

As explained in C# Concurrency: Asynchronous and multithreaded programming

A try-catch block is a C# exception-handling construct that catches exceptions raised by code in its try region.

A try-catch statement redirects control when code in its try region throws a matching exception. Runtime unwinding searches the call stack for an appropriate catch clause; without one, the program crashes. section 12.1

The source-code boundary is not the runtime boundary. Event-handler lambdas and continuations execute later in compiler-generated or callback methods, so exceptions thrown there do not reach the surrounding try block. Catch them where that deferred code runs, or propagate them explicitly. section 12.1

How C# Concurrency: Asynchronous and multithreaded programming covers it

How deep it goes
Explains call-stack unwinding and deferred callbacks, but stops short of detailed catch-filter or exception-type syntax.
Examples
Uses event-handler, await, ContinueWith, nested-catch, and TaskCompletionSource examples to contrast exception boundaries.
What it assumes
Assumes familiarity with methods, call stacks, lambdas, continuations, and async/await.
What you can do after
Diagnose why exceptions escape surrounding handlers and place catches at the executing asynchronous or callback boundary.
How it connects
Ties exception handling to call stacks, lambda expressions, event handlers, continuations, and async/await.

Also explained in

Compare how 10 books explain try-catch statement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C# Concurrency: Asynchronous and multithreaded programming. The section links point to the parts of the book it draws on.