Skip to content

About

Hands-on C# exceptions — mechanics, filters (when), throw vs throw e proven, custom exception design, measured best practices. Part 07 of a practice series.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

C# Exceptions — Hands-on Examples

Part 07 of a hands-on C# practice series. Four runnable, heavily commented examples: precise try/catch/finally mechanics (finally vs return, stack unwinding), exception filters (when) including the log-without-catching trick, throw; vs throw e; proven with stack traces, custom exception design done right, and a measured demonstration of why TryParse beats Parse+catch for expected failures.

Running

dotnet run        # all examples
dotnet run 2      # just one (e.g. filters)

What's inside

# Topic Highlights
01 Mechanics top-down catch matching, finally with return, unwinding, cost model
02 Filters & rethrow catch...when, log-and-keep-flying, throw; vs throw e; proven live
03 Custom exceptions type-as-API, data as properties, InnerException chains, when NOT to
04 Best practices empty-catch crime scene, Parse vs TryParse measured, guards, global handler

The review checklist to remember

  1. Never swallow — log and rethrow (throw;, never throw e;).
  2. Expected failure? TryXxx/Result<T>. Exceptional failure? Exception.
  3. Catch only what you can handle; one global handler catches the rest.
  4. Guard clauses throw next to the bug (ArgumentException.ThrowIfNullOrWhiteSpace).
  5. Custom exception only when a caller handles that case differently — and then: data as properties, wrap causes as InnerException.

Further reading

The series

← Previous: 06 Types & memory · Next: 08 Modern C# 8–12 →

Part Topic
01 Collections
02 LINQ
03 Delegates & events
04 Async/await
05 Generics
06 Types & memory
07 Exceptions (this repo)
08 Modern C# 8–12
09 Strings & text
10 Files & JSON
11 MiniShop microservices (finale)

Author

Matej Zlatunić

License

Released under the MIT License. Feel free to use it for learning.

About

Hands-on C# exceptions — mechanics, filters (when), throw vs throw e proven, custom exception design, measured best practices. Part 07 of a practice series.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages