Handling Exceptions in C# - When to catch them, where to catch them, and how to catch them

  • Loading...
  • Published on:  Friday, February 23, 2018
  • Handling exceptions is about more than just putting try/catch blocks in your code. Where you put them is very important. It is also important to know when not to put a try/catch in your code.

    This video covers the best practices of exception handling as well as how to rethrow exceptions, create new exceptions, and capture specific exceptions.

    Newsletter signup (with exclusive discounts): https://signup.iamtimcorey.com/ (your email will be kept safe and you will not be spammed).
    Source Code: https://leadmagnets.app/?Resource=ExceptionHandlingInCSharp

    0:00 - Intro
    0:39 - building a demo console application
    9:49 - crashing the application
    11:13 - "try-catch" and how NOT to use it
    15:00 - continuing with the exception & showing exception information to the user (15:57 )
    17:24 - more useful information: StackTrace
    20:31 - "try-catch": best place to put it and why
    27:23 - hanging logic demonstration
    30:31 - the "finally" code block and "throw" statement. Passing exception up the stack (to UI)
    38:08 - common misconceptions and mistakes
    40:25 - creating new exception without losing the original StackTrace info
    45:36 - recap
    47:05 - advanced exception handling
    53:09 - summary and tips on value capturing and handling exceptions
  • Source: https://youtu.be/LSkbnpjCEkk