Join Bruce Van Horn for an in-depth discussion in this video Cleaning up with finally, part of C# Essential Training: 2 Flow Control, Arrays, and Exception Handling.
- [Instructor] Sometimes, when you're operating…with a set of try-catch statements,…you want come code to execute no matter what.…For that, we can use the finally statement.…So I'm just going to tack this onto the end.…I have this try statement, and then…I have a series of three catch statements after it,…and I can add finally onto the end of this,…and the finally block runs no matter what.…So I'm just going to put Console.Writeline,…which wouldn't make sense in a real program,…but it will show you that this works as expected.…
So to test it, I'm going to go over to my C drive,…and I'll go into my Temp folder,…and I'll go ahead and just rename this file so that…we get a File Not Found exception, and I'll run this,…and I'll se that it couldn't find the file,…but the finally clause runs as well as the catch.…If I stop the program and fix the file,…now we're back to a condition where…the program will work normally.…
It reads the file, spits out the contents,…and then it runs finally.…So finally always runs, no matter what.…
AuthorBruce Van Horn
- Writing unit tests in C#
- Working with simple and multidimensional arrays
- Managing ordered and unordered data with lists
- Evaluating conditions with if-else statements
- Using OR, AND, and NOT operators
- Building loops
- Debugging and handling exceptions
- Creating the final build of your C# project
Skill Level Beginner
C# Object-Oriented Programming Tips and Trickswith Jesse Freeman58m 39s Intermediate
1. Unit Testing and Test-Driven Development
2. Arrays and Collections
3. Flow Control
4. Exception Handling
5. Getting to Production
Next steps1m 57s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.