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

  Рет қаралды 169,886

IAmTimCorey

IAmTimCorey

Күн бұрын

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): signup.iamtimcorey.com/ (your email will be kept safe and you will not be spammed).
Source Code: leadmagnets.app/?Resource=Exc...
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

Пікірлер: 433
@knockedgoose4206
@knockedgoose4206 2 жыл бұрын
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.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I am glad my content has been so helpful.
@DasturlashniOrganamiz
@DasturlashniOrganamiz 3 жыл бұрын
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!
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
You are welcome. I am glad it was helpful.
@korsos68
@korsos68 3 жыл бұрын
That was exactly the same that happened to me today. Really useful video. Thanks Tim.
@interiorecology
@interiorecology Жыл бұрын
This is such a good video. Beyond explaining exceptions, that surround with tool is gonna be so handy.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Great!
@haozheng8400
@haozheng8400 2 жыл бұрын
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.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I’m glad you are getting so much value out of it.
@bb579774
@bb579774 3 жыл бұрын
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!
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Great to hear!
@AlejandroHernandez-ru1zz
@AlejandroHernandez-ru1zz 9 ай бұрын
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.
@IAmTimCorey
@IAmTimCorey 9 ай бұрын
Thank you!
@octavianbreazu8486
@octavianbreazu8486 Жыл бұрын
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.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@karthiku1153
@karthiku1153 3 жыл бұрын
Thanks Tim. This gave a clear picture on how to handle exceptions and helped me to address code review comments today. Really a good one and can't wait to watch other videos.
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Glad it was helpful! Plenty of other videos posted out there. Please let me know what you think of them also.
@sanityinaseaofmadness7353
@sanityinaseaofmadness7353 3 жыл бұрын
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.
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Cool, thanks!
@RalfsBalodis
@RalfsBalodis 4 жыл бұрын
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
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Thanks! I added it to the description.
@RalfsBalodis
@RalfsBalodis 4 жыл бұрын
@@IAmTimCorey Thanks. Feel free to adjust some topic titles. I'm very new at c#. I might not have complete understanding how to describe something yet. Also, English is not my native language. I might not have the vacubolary ;)
@buskilamaor9587
@buskilamaor9587 3 жыл бұрын
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.
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
I am glad it was helpful.
@m1ch4l28
@m1ch4l28 3 жыл бұрын
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!
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Glad it helped!
@roadtoalaska-fuerzaucrania3549
@roadtoalaska-fuerzaucrania3549 2 жыл бұрын
This is the best youtube channel about C# !!!!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks!
@RealJustinEstrada
@RealJustinEstrada 4 жыл бұрын
best and most complete explanation on youtube, thank you sir.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You are welcome.
@Ruslakall
@Ruslakall 3 жыл бұрын
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"
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
lol
@SpesEtFides111
@SpesEtFides111 6 жыл бұрын
Very good as always. Thanks for all you do to help others, Tim!!
@IAmTimCorey
@IAmTimCorey 6 жыл бұрын
You are welcome.
@shaileshaggarwal8802
@shaileshaggarwal8802 5 жыл бұрын
OMG! I have been a developer for quite sometime now. But definitely did not know most of the basic you just described... Hands Down!!! Great video! I always learn something new from your videos.
@IAmTimCorey
@IAmTimCorey 5 жыл бұрын
Awesome!
@elikescode5753
@elikescode5753 6 жыл бұрын
I love when Tim says "Allz it doez". Keep up the good work.
@IAmTimCorey
@IAmTimCorey 6 жыл бұрын
lol, I'm glad you enjoyed it.
@brianmoore9281
@brianmoore9281 3 жыл бұрын
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!
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Glad it was helpful!
@DivakarRaj
@DivakarRaj 3 жыл бұрын
Amazing amazing video. Showed how elegant exception handling is !!!
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Thank you!
@niloo195
@niloo195 3 жыл бұрын
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!
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Glad to help!
@demps1bm1
@demps1bm1 8 ай бұрын
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!
@jonathanmcdermott25
@jonathanmcdermott25 4 жыл бұрын
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!
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Excellent! I'm glad my content is helping.
@fcu423
@fcu423 5 жыл бұрын
Tim your videos are extremely great! Thank you and keep it up!
@IAmTimCorey
@IAmTimCorey 5 жыл бұрын
Excellent! I'm glad you are enjoying them.
@techbrojoe
@techbrojoe Жыл бұрын
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.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Glad it was helpful!
@roquefigueroa4916
@roquefigueroa4916 5 жыл бұрын
Thank you for creating this video. This helped me understand exceptions a lot better.
@IAmTimCorey
@IAmTimCorey 5 жыл бұрын
You are welcome.
@BijouBakson
@BijouBakson 2 жыл бұрын
This is as far as I am concerned, very thorough and net. Subscribed. Thank you so much.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@nc3768
@nc3768 2 жыл бұрын
I dont comment on videos but i had to write a comment after watching it today for the second time just to fully comprehend. Love your detailed explanation and love the video tags as well: it helps when coming back. Recap is the best part of this video after watching it in the past but you want the 2 minutes explanation.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I am glad it was helpful.
@Jose67898
@Jose67898 2 жыл бұрын
It is a super clear explanation about try-catch. Thank You!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@facundovega1726
@facundovega1726 2 жыл бұрын
Amazing content as always. thanks for sharing all your knowledges Tim!. Regards from Argentina.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thank you!
@marcocambon
@marcocambon 4 жыл бұрын
Hi Tim, nice and clear explanation that immediatly highlights the main common mistakes. Also very clear english even for an italian :)
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Thank you!
@user-pz1cd8lv3n
@user-pz1cd8lv3n 7 ай бұрын
Thanks for amazing through content. you explain with simple examples the little details that come with the experience of the years.
@IAmTimCorey
@IAmTimCorey 7 ай бұрын
You are welcome.
@goji5887
@goji5887 6 жыл бұрын
Definitely one of the most useful videos, thank you very much!
@IAmTimCorey
@IAmTimCorey 6 жыл бұрын
I am glad you think so. Thank you.
@CantChooseNic
@CantChooseNic 6 жыл бұрын
Thank you for making these practical videos!
@IAmTimCorey
@IAmTimCorey 6 жыл бұрын
You are welcome.
@Trashcandogchoir
@Trashcandogchoir 3 жыл бұрын
Finally someone with a tolerable voice and cadence. Very soothing yet keeps the attention! Great work!
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Thanks!
@rockymarquiss8327
@rockymarquiss8327 3 жыл бұрын
I agree - Tim communicates effectively and clearly. He is a good teacher and does a great jo.
@shrirangpinjarkar
@shrirangpinjarkar 4 жыл бұрын
Thanks a lot man. Very well explained best practices for exception handling.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You are welcome.
@PaulMcKillop
@PaulMcKillop 6 жыл бұрын
Another really good video, Tim. Many thanks.
@IAmTimCorey
@IAmTimCorey 6 жыл бұрын
I am glad you enjoyed it.
@ernstpeterlegrand
@ernstpeterlegrand 4 жыл бұрын
Thanks again, gives me for sure a better understanding of exceptions.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You are most welcome. Thanks for watching.
@goodmanshawnhuang
@goodmanshawnhuang 2 жыл бұрын
Thanks a lot Tim, I often eat exceptions and will stop doing that 😂. really love the way you explained it.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@dypcio3687
@dypcio3687 5 жыл бұрын
It's a very helpful video. Thank you Tim !!
@IAmTimCorey
@IAmTimCorey 5 жыл бұрын
You are welcome.
@sheznabiel985
@sheznabiel985 3 жыл бұрын
Great video, good pace and easy to follow! Subbed! Thanks
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
You are welcome.
@AliMustafa-xp8ih
@AliMustafa-xp8ih 7 ай бұрын
what a great tutorial , Thank you TIm
@IAmTimCorey
@IAmTimCorey 7 ай бұрын
You are welcome.
@amineayoubiIT
@amineayoubiIT 5 жыл бұрын
Best explanation ever seen , keep going sir .
@IAmTimCorey
@IAmTimCorey 5 жыл бұрын
Thank you!
@mihowbogucki4928
@mihowbogucki4928 5 жыл бұрын
Thank Tim! Love your videos
@IAmTimCorey
@IAmTimCorey 5 жыл бұрын
You are welcome.
@goverdhanjayaram3683
@goverdhanjayaram3683 5 ай бұрын
Thank you very much Tim! Very well explained!
@IAmTimCorey
@IAmTimCorey 5 ай бұрын
You are welcome.
@vijeshkanth4604
@vijeshkanth4604 4 жыл бұрын
Hi Tim, it's been great to watch out your videos for learning any C# basics and advance topics. You make the things so better and understandable.It's important to realise how to write a proper code, I am glad to you for that. Can you please make a video on File Operations also. Thanks in advance.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I will add it to the list. Thanks for the suggestion.
@vinuhosanagar1
@vinuhosanagar1 6 жыл бұрын
Thank you so much Sir. You are always the BEST
@IAmTimCorey
@IAmTimCorey 6 жыл бұрын
You are welcome.
@Greebstreebling
@Greebstreebling 2 жыл бұрын
Thank you very much for this clear and helpful explanation, really useful.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@davidramirez3322
@davidramirez3322 4 жыл бұрын
Thnks so much Tim !! a great video as usual!!
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You are welcome.
@karimmessaoud8223
@karimmessaoud8223 2 жыл бұрын
Very good explanation. Thank you.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@mukunan6171
@mukunan6171 5 жыл бұрын
I really appreciate for your explanation!
@IAmTimCorey
@IAmTimCorey 5 жыл бұрын
Thank you!
@mohammadranjbaran1897
@mohammadranjbaran1897 2 жыл бұрын
Thank you very much Tim.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@Tarodev
@Tarodev 5 ай бұрын
I've always pointed people to this video, so I suppose I owe you, thanks.
@IAmTimCorey
@IAmTimCorey 5 ай бұрын
Thank you! Although, I don't consider you to owe me anything. I'm happy to be of help.
@wafaza7934
@wafaza7934 2 жыл бұрын
thanks, bro your explanation is excellent
@tomthelestaff-iamtimcorey7597
@tomthelestaff-iamtimcorey7597 2 жыл бұрын
Thanks for the encouragement and for faithful support
@LuisEduardoHernandezT
@LuisEduardoHernandezT 5 жыл бұрын
Instant like & subscribe! So simple explained. Basic stuff, yet how common it is to use exceptions the wrong way.
@IAmTimCorey
@IAmTimCorey 5 жыл бұрын
I'm glad you enjoyed it.
@dyachenkoserj
@dyachenkoserj 5 жыл бұрын
Another great video. Thanks!
@IAmTimCorey
@IAmTimCorey 5 жыл бұрын
You are welcome.
@Brokdarify
@Brokdarify 6 жыл бұрын
Very nice explanation!
@IAmTimCorey
@IAmTimCorey 6 жыл бұрын
Thank you.
@swapnilpakolu4395
@swapnilpakolu4395 Жыл бұрын
Thanks for great explanation!!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@ChiefsFanInSC
@ChiefsFanInSC Жыл бұрын
This is a great video!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@jacanaro5735
@jacanaro5735 Жыл бұрын
exactly what i needed
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Great!
@johng5295
@johng5295 Жыл бұрын
Thanks in a million.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@papillonx101
@papillonx101 5 жыл бұрын
Superior explenation, as always.
@IAmTimCorey
@IAmTimCorey 5 жыл бұрын
Thank you!
@michaelvenero1509
@michaelvenero1509 2 жыл бұрын
Great video thank you.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@gonmigamez9072
@gonmigamez9072 4 жыл бұрын
yooooooooooooooooo you are amazing dude you literally made my life easier you are actually my favorite
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Awesome!
@anagabrielareyna1549
@anagabrielareyna1549 6 жыл бұрын
Great video, it helped me a lot.
@IAmTimCorey
@IAmTimCorey 6 жыл бұрын
Awesome!
@mashinc5425
@mashinc5425 5 жыл бұрын
Hey Tim. Great Video series. Instead of looping through all InnerExceptions you can simply call ex.ToString(). It already returns "message" and all InnerExceptions.
@IAmTimCorey
@IAmTimCorey 5 жыл бұрын
Yep. The only downside is that you have to slog through a bunch of text then.
@harvey_04
@harvey_04 11 ай бұрын
Thanks for the video
@IAmTimCorey
@IAmTimCorey 11 ай бұрын
You are welcome.
@CelioMotta
@CelioMotta 5 жыл бұрын
Great, thanks!
@IAmTimCorey
@IAmTimCorey 5 жыл бұрын
You are most welcome. Thanks for watching.
@guntbert9709
@guntbert9709 6 жыл бұрын
Thank you Tim, again. A very nice introduction to exceptions - amazing how you pack all that into not quite an hour! Allow me just one remark: you don't need `catch` when you just want `finally`, there exists even a snippet `tryf`.
@IAmTimCorey
@IAmTimCorey 6 жыл бұрын
Thanks for the tip. I should have pointed that out. I wanted to highlight that you could do logging, etc. in your catch but I forgot to mention you could skip the catch if you just wanted the finally. Good catch (see what I did there?)
@zvone9035
@zvone9035 4 жыл бұрын
Thank you very much :D
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You are welcome.
@octavian9168
@octavian9168 2 жыл бұрын
Thanks!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thank you!
@rskandari
@rskandari 3 жыл бұрын
Awesome tutorial
@tomthelestaff-iamtimcorey7597
@tomthelestaff-iamtimcorey7597 3 жыл бұрын
Thanks for trusting Tim for your training.
@Jose67898
@Jose67898 2 жыл бұрын
Köszönjük!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thank you!
@vkpunique
@vkpunique 4 жыл бұрын
Very Helpful
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Great!
@Ruslakall
@Ruslakall 3 жыл бұрын
Worth mentioning that you only get the "in" part of the exception stacktrace if you are debugging and have the .pdb file with the executable. I've actually copied the .pdb files by mistake into production environment and later seen a reference to my local machine in a logged exception :)
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Thanks for sharing.
@Barrosy
@Barrosy 2 жыл бұрын
I had some fun discussion with one of my teachers about the fact that using exception handling may cause your application to do more work than it's supposed to. Exceptions themselves are classes containing a bunch of logic, which the application requires time and processing power to go through. Sometimes it might actually not be feasible to use exception handling. My question then would be, what are good examples/scenarios of when to refrain from using exception handling? The discussion was long ago and I like to watch your videos as refreshers/reminders.
@fadidib8516
@fadidib8516 2 жыл бұрын
wothout exception handling how is it even possible to fail gracefully does not make any sense
@DanneManne88
@DanneManne88 Жыл бұрын
Verry good video! :)
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@gangstaqualified6777
@gangstaqualified6777 10 ай бұрын
Very good video
@IAmTimCorey
@IAmTimCorey 10 ай бұрын
Thank you!
@LeonardoDaudinot
@LeonardoDaudinot 6 жыл бұрын
Thanks again
@IAmTimCorey
@IAmTimCorey 6 жыл бұрын
You are welcome.
@joeyvico
@joeyvico 3 жыл бұрын
Brilliant! New subscriber here!
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Thanks and welcome
@alanbaird4543
@alanbaird4543 2 жыл бұрын
Thanks
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thank you!
@MusicToMyEar1005
@MusicToMyEar1005 4 жыл бұрын
This is what I learned from this: Coding of the exception handling part is as important as the coding of the "main stream" of the program. It is, as if, I say, writing the code is something like a drawing of a picture on the front side of the canvas. and the writing the exception handling part is like a drawing on "the back side of the canvas"!! You can't complete your art, until you complete the both side of the canvas.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Interesting way of looking at things.
@edwardmullan2724
@edwardmullan2724 4 жыл бұрын
I would say it's like wiring a house, you need to put fuses and circuit breakers in so the whole thing doesn't burn down when you fire up you faulty toaster
@harag9
@harag9 6 жыл бұрын
Excellent & clear tutorial, thanks Tim. Will you be doing a more advanced lesson for Exception Pattern Matching in C#7?
@IAmTimCorey
@IAmTimCorey 6 жыл бұрын
That's a firm maybe. I love the new feature and it would make a good little video. I'm just not sure when I'll fit it in. I did mention pattern matching in my Top Five C# 7.0 Features video but not specifically as it relates to exceptions I don't believe.
@michaelblair5146
@michaelblair5146 3 жыл бұрын
If anyone is wondering at 8:11 why their code isn't allowing them to access the Reference Library as a possible 5th option, That is because you forgot to add 'Public' access modifier to the DemoCode Exceptions Library project class.
@tomthelestaff-iamtimcorey7597
@tomthelestaff-iamtimcorey7597 3 жыл бұрын
Thanks for sharing that. I hope it helps others
@mohamedmp5255
@mohamedmp5255 4 жыл бұрын
great thank you
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You are welcome.
@arpitajoshi190
@arpitajoshi190 2 жыл бұрын
Thank you for this helpful video! Was wondering if you could also do a video on what makes a program execution stop? Like 'break' etc keywords
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/
@arpitajoshi190
@arpitajoshi190 2 жыл бұрын
Done. Thank you!
@fiddler-dv4or
@fiddler-dv4or 4 жыл бұрын
Another great tut, Tim. Thank you! I did not think to loop through exceptions looking at inner exceptions. great tip. Have you done an advanced exception handling video? For MVC apps specifically? Love to see your take on that also.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I haven't but I can add that to the suggestion list.
@lauriedickinson5373
@lauriedickinson5373 3 жыл бұрын
Very helpful video! BIggest bit of new info for me is difference between throw; and throw ex; Thanks!
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Glad it was helpful!
@PabloMateoDrappo
@PabloMateoDrappo 2 жыл бұрын
Amazing video. Very useful! Thanks Tim. I click lik, comment and suscribed.. I do my homework. haha
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Awesome, thank you!
@ramonchitobendijo3465
@ramonchitobendijo3465 3 жыл бұрын
Actually, you can remove the catch in the GrandParentMethod and it will still be caught by the UI (Main method which calls the GrandParentMethod). I also want to point out that the catch in the grandparent method is called even before calling finally. The catch method however in the UI is called after the finally block hence the reason why you can see that DB connection is closed before Console.Writeline in the UI is shown.
@zedsmelee
@zedsmelee 2 жыл бұрын
Wasn't this done simply for display purposes opposed to best practice?
@mateuszryszkaniec7345
@mateuszryszkaniec7345 4 жыл бұрын
Hi Tim, you mentioned about calling method from different environment like MVC or WPF etc (not having console) - I was hoping here you will recommend to use a "response" & "request" messaging classes where response base contains (bool IsSuccesffull, string Message) :)
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I don't typically use something like that. Instead, I use ILogger for logging messages and exception messages for responses sent up the chain.
@rodcka
@rodcka 3 жыл бұрын
Great explanation, Tim! I am wondering where should I put my custom exceptions when I'm working in a layered solution. Should they belong to the business layer (since they are related to the business rules), or should they belong to the presentation layer (razor, api etc) since they could be caught near the UI?
@krsnaviation
@krsnaviation 3 жыл бұрын
Thank you very much. This was very helpful. One more sub for you! =)
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Thanks for the sub!
@shah10955
@shah10955 6 жыл бұрын
Great video. Thanks. Perhaps a little discussion could be included on what happens when UI calls a service via Web Api etc.
@IAmTimCorey
@IAmTimCorey 6 жыл бұрын
In what way? Like what happens if the call fails? Or if the API throws an exception?
@shah10955
@shah10955 6 жыл бұрын
If the api throws exception.
@amortalbeing
@amortalbeing 2 жыл бұрын
Thanks again, it would be a good idea if you could mention that throwing/handling exception is an expensive operation and its best to always try to avoid one (raising exception), unless proven otherwise.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Exceptions can be expensive but broken applications can be even more expensive. I did mention that exceptions are expensive in this video, but there is a balance here. I'm not prepared to say that the best thing is to avoid them. Throwing an exception is the right thing more often than that. Otherwise, you introduce potential vulnerabilities into your application.
@amortalbeing
@amortalbeing 2 жыл бұрын
@@IAmTimCorey I guess I didn't convey myself well, I as well do not mean not to ever use them. When I said one should strive to not use exceptions as much as possible, was to proactively be wary of situations that can result in an exception and prevent them as apposed to let it happen and manage it in an exception handler. For example, instead of accepting all forms of an input and then resorting to a try catch instead of proper validation is clear example of such misuse that I was trying to ward off. I guess, I'd call this kind of exception handling, sweeping one's irresponsibility/laziness under the rug.
@BijouBakson
@BijouBakson 2 жыл бұрын
@@amortalbeing I believe preventing the user from entering bad data is subject to training or familiarity with the system. You cannot guarantee that all your users are properly trained, especially if you're dealing with an application that is available on the internet. If you mean entering a code that only allows the user to enter some type of data, that is literally error handling. Maybe you were referring to using other methods that try-catch?
@mikemac7408
@mikemac7408 3 жыл бұрын
Little tip for those interested. Xamarin Workbooks is great for learning APIs and practicing concepts in isolation. You can write nicely formatted markdown alongside your code to document and explain your reasoning process. You can then save and share your work in a single, self contained file.
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Great tip!
@victorkung8922
@victorkung8922 3 жыл бұрын
Hi Mr. Tim Corey. This is your first video I watch and I feel it's really great. I start to understand the usage of try catch. I'm a c# beginner and I still have queries. As you said we aren't putting try catch block every where. We usually put try catch at somewhere we think there may be trouble or issue arise with unexpected user behavior. My question is, if we knew there would be a problem somewhere, what's the different between we put try catch and we do some checking? For your example, you use try catch to capture the exception while user put in demo.GrandParentMethod(4) which the argument is out of the index range. If we knew that there could be bad user providing input that's out of range, we can do something like if (position >= numbers.length -1) { // do something in U.I. to notice user about wrong input }. In this case it's not necessarily to use try catch right? Thanks for your great job. I will go on to watch all your videos that I need, and followed your podcast in Spotify too. Hope I will be a great developer as you some day.
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Great question. Depending on the circumstances, identifying the potential problems and checking for them can be a great option. However, sometimes an exception is the right call because the application should not continue as if everything is normal. Take a method called IsOver18() for example. Maybe it checks the value of a file on disk to see if the person in that file is over 18. What value should it return if the file does not exist? Normally, it would return true for 18 or older and false for under 18. If the file does not exist, neither of these apply. If it forced one of the choices, we might make decisions based upon that information, which would be incorrect. That is why exceptions exist - the application should not continue as if everything is ok. That's why you would need to catch the exception if you were calling this method. Whenever the application gets in an unexpected state, an exception should probably be thrown. We don't always have the ability to pre-validate the data.
@victorkung8922
@victorkung8922 3 жыл бұрын
@@IAmTimCorey Thank you very much Tim. I'm going to attend some of your lesson in your website. I really need to consolidate my foundation for a long journey on c#.
@mggray1971
@mggray1971 3 жыл бұрын
Hi Tim. Thanks for the Video this has helped a great deal in where to consider for utlising try/catch blocks. I have a further query which relates to the different types of exception filters that are available from the Exceptions class library. When you are designing code what makes you choose specific exceptions filters over the generic exception library. If this generic filter provides you with the correct error output why would you need to choose specific filters. I can see you have used the ArgumentException but this you threw yourself. Why and how would I use specific filters in code when you may be unsure as to what error could be generated by the code. Thanks in advance Mike
@jeremiahmedel9608
@jeremiahmedel9608 3 жыл бұрын
We can't fix users, wish we could -Tim Corey
@tomthelestaff-iamtimcorey7597
@tomthelestaff-iamtimcorey7597 3 жыл бұрын
Those pesky users are what keep us employed.
@duke227
@duke227 5 жыл бұрын
Hi Tim, great video as always. Would be great to have a video that adds on handling exceptions when using async programming as well as maybe using events to get ui to display progress and exceptions. Does try catch return path work across async when method is running on a different thread than UI?
@IAmTimCorey
@IAmTimCorey 5 жыл бұрын
Handling exceptions in async is mostly straightforward. You simply wrap the top level async call in a try catch and it will capture the exception on the correct thread. The two things you need to ensure (which you should anyway) are that the async call returns a Task (not void) and that you await the results.
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 170 #shorts
00:27
Always be more smart #shorts
00:32
Jin and Hattie
Рет қаралды 42 МЛН
Intro to Unit Testing in C# using XUnit
1:42:09
IAmTimCorey
Рет қаралды 409 М.
Beginner's Guide to Exception Handling in C#
29:46
Nick Proud
Рет қаралды 3,7 М.
Exception Handling | C# | Tutorial 24
11:01
Giraffe Academy
Рет қаралды 86 М.
Delegates in C# - A practical demonstration, including Action and Func
1:09:11
C# Events - Creating and Consuming Events in Your Application
1:09:13
Golang Tutorial for Beginners | Full Go Course
3:24:59
TechWorld with Nana
Рет қаралды 1,8 МЛН