Try...Catch and Application

Objectives


Discussion

Application Class

Try...Catch...Finally

Try
   [Statements here ]
Catch ex as exception 'ex as exception is optional
   [Statements here ]
Finally 'optional
   [Statements here ]
End Try

Back to top


Demonstration

In this demo we will modify the demo that you created in the last lesson on FileStreams and incorporate Application.StartupPath and Try...Catch.

1.  Open your Unit 7 project.  In this program we hard-coded the location of the saved file.  By hard-coding we mean that you typed in the path and filename in the statement in your program.  Hardcoding a path can be a problem if you want to use your program on other computers.  If the path that you hard-coded is not on the other computer then your program will crash when it tries to read the file.  One way to resolve this issue is to put the file in the startup path for your program and then use Application.StartupPath.  This is what we will do. 

2.  Let's first see what Application.StartupPath does.  Add a button (btnStartUpPath) and add the following code to the click event for this button:

Messagebox.show(application.startuppath)

3.  Run the program and click the button.  Notice the startup path for your project.  The startup path is where the .exe file for your project is located.

4.  Now we will use Application.StartupPath in your read and write routines.  In your click event for btnWrite, modify or replace your code so that it looks like:

Dim sFile As String
sFile = Application.StartupPath & "\test.txt"
Dim swWriter As New StreamWriter(sFile, False)
swWriter.WriteLine(txtFile.Text)
swWriter.Close()

5.  Run the program.  Write out a file.  Then use Notepad to open the file.  Remember that your file is in the startup path for your program. If you forget where that is, run the program and click btnStartUpPath.

6.  Now we will modify the code in btnRead to use Application.StartUpPath.  Modify the code so that it looks like the following.  Notice that we have abbreviated the code by combining multiple statements and using the ReadToEnd method. We did this just to show you that there is generally more than one way to do something and you can often find a way to simplify your code.

Dim srF As New StreamReader(Application.StartupPath & "\test.txt")
txtFile.Text = srF.ReadToEnd
srF.Close()

7.  Run the program and read and write files. 

8.  Now we will investigate Try...Catch.  There are situations that can cause your program to crash while it is running.  Using Try...Catch can prevent these crashes.  One situation that could cause this demo program to crash is if you try to read a file that does not exist.  Let's demonstrate.  In the line that declares the StreamReader, change the name of the file to a nonsense name of a file that does not exist.

Dim srF As New StreamReader(Application.StartupPath & "\xyz.txt")

9.  Run your program and click btnRead. You should get an error stating that the file could not be found.  One way to solve this problem is to use the File System Object to determine if the file exists.  Unfortunately, we are not going to discuss this object.  Instead we will use Try...Catch.  Modify your code in the click event for btnRead to look like:

Try
   Dim srF As New StreamReader(Application.StartupPath & "\xyz.txt")
   txtFile.Text = srF.ReadToEnd
   srF.Close()
Catch ex As Exception
   MessageBox.Show(ex.Message)
End Try

10.  Run your program and click the read button.  You should get a message to indicate the error but the program does not crash.  You could write code to do something else in the Catch portion.  Also we did not include the Finally statement.  There are situations where you might want to use the Finally statement.

Back to top
Exercises

1.  Write a program that lets a user save a list of numbers to a file.  Your program should meet the following requirements:

  1. The user should be able to add numbers to a listbox or multi-line textbox.
  2. The user should be able to enter a name for the file and save the numbers in the file.  This will allow the user to save multiple files.
  3. The file of numbers should be saved to the startup directory.

2.  Write a program (or modify the program in #1) that lets the user open and read in the files of numbers from #1.  You program should meet the following requirements:

  1. The user should be be able to enter a filename and read the numbers from the file into a listbox or multi-line textbox.
  2. The program should not crash if the user enters an invalid filename.
  3. A means should be provided to calculate and display the average of the numbers.
Back to top
Links & Help
Back to top