.NET developer

Stage 1 · Lesson 7 of 7

Exceptions and disposing resources

C# and the .NET platform

When code meets a situation it cannot handle, it an exception, and the nearest matching catch up the call stack deals with it. Catch only exceptions you can do something useful about, such as a concurrency conflict you turn into a 409 response.

Let everything else travel up to one global handler, which you will add in the production stage. using and await using make sure connections, files and are disposed, even when an exception is thrown. Custom exceptions such as InsufficientFundsException make business failures clear.

Exceptions are for exceptional cases: validation failures are better returned as than thrown on every bad request.

Common trapthrow ex;, which resets the stack and hides where the problem started. Write throw; instead.

A question to check yourself, an AI practice session and 2 cheat sheet entries come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

  • GuideFree
    Learn C#

    Microsoft · Beginner

    Free official C# path: syntax, types, classes, collections and exceptions with in-browser exercises

  • GuideFree
    .NET documentation

    Microsoft

    The official .NET docs: start with the CLI overview, project files and the C# language tour

Practise

  • The free C# track: small exercises with mentor feedback that build fluency fast

Keep handy

  • Searchable offline docs, handy for C# and .NET API lookups while you practise

Also worth your time

Mark this stage as not done?