Content Archived

This content is no longer current. Our recommendation for up to date content:

Gracefully Handling Exceptions - 20

Play Gracefully Handling Exceptions - 20
Sign in to queue


Exceptions occur when an application experiences some unexpected problem at run time. This lesson discusses how to use the try catch finally block to anticipate potential problems and attempt to shield the end user from those problems as much as possible. We discuss best practices when checking for exceptions and discuss the mindset of the conscientious software developer who seeks to provide the best possible experience for his users.

Download the source code for Gracefully Handling Exceptions



C#, Visual Studio



The Discussion

  • User profile image

    I liked this video because it offered technique as well as theory and motive. Instead of just showing me how to handle exceptions so that I'm pleased with my software, it went on to explain why that should be a priority in my coding practices: I'm the advocate for my customer. Carrying that to conclusion:  if my customer is unhappy, I've both angered somebody and lost a customer. Both could be avoided by being conscientious in my coding.

  • User profile image

    @a14437: Awesome, thanks for that feedback.  I try to be empathetic in my explanations ... sometimes I hit and sometimes I miss.  Glad I "hit" on this one.  Big Smile

  • User profile image

    It would be nice if you proceed with our casts and cover every basic aspect of C#.
    Covering casting, generics, delegates, events etc.
    Just like an ordinary programming book.
    That would be really great. I wish you would find a time and effort for it :)
    Very nice casts though. Thanks a lot!

  • User profile image

    @Paul:  My friend, sounds like you're already way beyond this course.  Smiley  It was intended for absolute beginners.  The innards of delegates and events, and even generics to an extent is not a beginner topic.  I do have videos demonstrating those topics on my own website, however it was out of scope for this one.  Thank you and best wishes!

  • User profile image

    Thanks for the response :)
    Im just reading "Pro C# 2010" by Troelsen and it seems he is covering it in this way. I've just passed Exceptions topic in the book and trying to fix it in my mind by watching your casts :)
    Found even that you explained it much better than he does. I had been a little confused by his examples. :)
    Can you share your personal website here on send the link to my email?
    I would really appreciate that. Thanks a lot!

  • User profile image

    Thanks a million for this. Found what i had been looking for. Would like to go beyond this... on to ultimate expertise in practice! Advize...

  • User profile image

    Thanks for this tutorial, it was pretty clear.
    However, I have a question: Since you included the finally part, where, as you said, we release connections to files, shouldn't you move the myReader.Close() in that finally block?

  • User profile image



    @Andrew: On my own website I've got an extended version (22 hours) of this series complete with exercises & video solutions to challenge you.  It is so popular that moving forward, I'm structuring all new content around challenges / exercises / missions /etc.  Besides "instructor created challenges", the best way to get all of this solid in your mind?  Write code!  Smiley  Dream up a project and create it!  It will force you to think on your own feet, and struggle -- hopefully a positive / good kind of struggle (as opposed to the kind where you're just spinning your wheels and getting no where).

    @Sebastian: Yes, probably should have ... I don't have the code handy here right in front of me, but that would be a good place for it.  I *may* have been thinking it's possible for .NET to croak on closing the file, in which case, putting it in the finally would throw a second exception, this time, not caught.  But generally, yes, you are correct.  Smiley 

Add Your 2 Cents