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

preview_player
Показать описание
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.

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
Рекомендации по теме
Комментарии
Автор

This channel has been a huge help to me as a beginner programmer. I picked up a book on C# and although it outlines a good path of learning, and has taught me a lot of basic concepts, it really doesn't go into the "why" of a lot of things.

Many thanks Tim, it is clear that you are a mentor to many, and I appreciate that you put out tons of free content like this to help people improve.

knockedgoose
Автор

You were explaining bad practices with handling exceptions in the beginning.

Me: That's exactly what I have been doing till this moment 🤦‍♂️

Thanks, Mr. Corey for taking time for new developers like me. I really appreciate your content!

DasturlashniOrganamiz
Автор

This course is so so so great!!! Thank you so much!
1. It's better to let your application crash than eat the error and leave your code in an unknow state.
2. Put the "try...catch..." as high as possible so we can get the full call stack message in stack trace.
3. The difference between "throw" && "throw ex" && "throw new Exceptions("message")"
4. Different type of exceptions(eg: ArgumentException)
5. If we have multiple catch in one function, it will come to the first catch that matches.

haozheng
Автор

As someone who's been working with C# for more than a decade, I have fun watching your material in this channel. I usually pick up 5-10% of it as corrections-to or suggestions-for the things I've been doing. I even learn something completely new to me from time to time. Nicely done, Tim.

sanityinaseaofmadness
Автор

Before watching this video, I was the one you mentioned in 37:00, who does not know the real purpose of catch! Thanks Tim! I am learning and learning and enjoying from your videos! Cheers!

niloo
Автор

This is such a good video. Beyond explaining exceptions, that surround with tool is gonna be so handy.

interiorecology
Автор

Wow, the best and the most detailed explanation (and non dumb "DivideByZero" exception explanation) which actually explained the whole stack call chaining and different reasons to throw \ catch \ handle exceptions...thanks man.

buskilamaor
Автор

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 loosing original StackTrace info
45:36 - recap
47:05 - advanced exception handling
53:09 - summary and tips on value capturing and handling exceptions

RalfsBalodis
Автор

This is the best youtube channel about C# !!!!

roadtoalaska-fuerzaucrania
Автор

Until now I often catched exceptions at low level, but now I understand the problem and benefits of doing it at high level.
That was really helpful, once again. Thanks!

stanik
Автор

Finally someone with a tolerable voice and cadence. Very soothing yet keeps the attention! Great work!

Trashcandogchoir
Автор

Hi, current CS major with the world’s driest textbook for an online C# class here. Thank you SO much for your wonderful videos. There is something magical about seeing something in practice vs reading a hundred pages about exceptions.
Getting a base understanding from your videos before trying to comprehend certain topics makes life WAY easier!
Great channel. Glad to be a new subscriber!

jonathanmcdermott
Автор

You missed out on a perfect pun. When you stated you didn't like abbreviations on variable names, you should have said "...but ex is the exception"

Ruslakall
Автор

I've always pointed people to this video, so I suppose I owe you, thanks.

Tarodev
Автор

Another really fantastic video @IAmTimCorey. I am learning server side C# right now and your videos are always the first place I look. Thank you!

bb
Автор

Thanks for the video! I understood Try/Catch/Finally at a fundamental level and implemented them before. However, the throw; versus throw ex; and how the stack traces work depending on where you have the try/catch was something I never realized until watching this video and your explanation.

techbrojoe
Автор

Thanks Tim, as ever a very informative video. I am an experienced c# developer but even so its worth watching your videos as every now and again you learn something new - and in this case I discovered I have not been using the Finally clause properly!

brianmoore
Автор

One of the most effective and nice presentations I have watched. I return to it from time to time to refresh my mind.
Thank you.

octavianbreazu
Автор

As always a great content Tim. This is what I like your videos, you don't just show how to use things, you also explain with simple examples the little details that come with the experience of the years.

AlejandroHernandez-ruzz
Автор

Great video. I've known the mechanics of try/catch forever, but not the best practices, which you covered. One thing that you can do with try/catch is dealing with expected errors. I had a case where I had a text file of over a million records, of which 70%+ I knew would be defective. It was a console app that I threw together quickly to separate good data from bad. I used try/catch to log the error records but it also enabled me to keep my app running. In this case, I did not want the app to stop at all, even if there was an error (and in this case some 700k or more records were, as expected, bad.) Anyway, all of your advice is exactly right -- especially throw; which maintains the critical call stack. Thank you so much!

dempsbm