 Let's modify the program that sorted the names of actors by last name and change it so that the user can specify the name of the input file to read from and the output file to write to. I've already put all of the program into a main function just for convenience. Our first change is going to be on line 9. Instead of opening actors.txt, I'm going to call getInputFile to ask the user. Here's that function. I'll define getInputFile to say filename equals input, enter inputFileName, and then inputFile equals open whatever that filename was for reading and return the file handle. If I put in a valid filename, like actors.txt, everything works fine. But if I put in the name of an invalid file, instead of crashing the program, let's use try and accept. We'll set valid to false, and while not valid, I'm going to try getting the filename and opening it. And if there's an error, I'll print error opening filename. I also have to set valid to be true if it opens successfully, otherwise I have an infinite loop. Now let's run the program, and this time if I put in noSuchFile.txt, nonexistent.txt, it'll only continue if I put in actors.txt, and everything worked great. I'm going to do something similar for the output file. In this case, instead of always writing to actorsorted.txt, I'll call a function called getOutputFile, which asks the user for the filename to write to. And since I don't know in advance what that's going to be, I'll just say file written here at the end. And now the function. It's going to be very similar to getInputFile, and I'm going to use try and catch to make sure that an invalid name doesn't crash my program. I'll set valid to false, while not valid, try asking for a filename. This time my output file will be open of whatever name you gave me for writing, and if it was successful, everything's valid. Otherwise, let's give the error a message that we would get from the operating system by saying exception as error, print the error straight from the horse's mouth, and then return output file. Let's clear the shell and run the program. If I give a bad filename, it doesn't let me get by. I'll say actors.txt, which is a valid name. For a non-existent filename, I'll say non-existent slash nofile.txt. And it says there's no such file a directory. If I say new sorted file.txt, which is a valid name, it wrote the file, and I can check that by opening new sorted file.txt, and sure enough, that worked great. There's one more place for me to put a try and accept, and that's while I am writing the file. That could run into an error. Let's say if I ran out of disk space, or if there was a bad sector on the disk and the program crashed because it couldn't write it properly. I'd like to be able to catch those errors as well. I'll do that by putting a try before the loop that writes my output and an accept exception as error, because I want to see the exact error that I got. I'll print error encountered, writing output file, and then I'll print the direct error message from the operating system. I'm going to add one more clause here. It's called the finally clause. The finally clause is executed whether the try succeeded or not. If the try succeeded, it'll go to the finally. If the exception happened, it'll still do the finally, and that's where I'm going to do my output file.close. This guarantees that whether I had an error or not, whatever did get written will be saved to disk, and I don't need that line anymore. I'd like to show you this error in action, but I don't know how to cause a hard disk error, and frankly, I wouldn't want to, so you'll just have to take it on faith that this works. At any rate, that's how you use try and accept with programs that read and write files to make sure they handle errors without crashing the program.