Download presentation
Presentation is loading. Please wait.
Published byAnnice Foster Modified over 9 years ago
1
Error Trapping Exception Catching 1
2
Syntax & Compile-time VB – Editor/Compiler Logic and Design You Runtime You Types of Errors: 2 When your program bombs!
3
When performing data validation, some errors are easy to detect and respond to. Remember our initial interest program: 3home 100 3 5
4
4 IsNumeric Private Sub (…) Handles cmdCalculate.Click lblResult.Text = txtPrin.Text * txtRate.Text * txtTime.Text End Sub Private Sub (…) Handles cmdCalculate.Click If (Not IsNumeric(txtTime.Text)) Then msgbox(…) txtTime.SelectAll() txtTime.Text = “” Exit Sub End If lblResult.Text = txtPrin.Text * txtRate.Text * txtTime.Text End Sub Of course, we would have to do this for each of the TextBoxes. Note: this code also handles an empty TextBox. Of course, we would have to do this for each of the TextBoxes. Note: this code also handles an empty TextBox.
5
Structured Exception Handling 5 Common Exception Classes ExceptionCaused By FormatExceptionFailure of a numeric conversion, such as Integer or Parse. Usually blank or nonnumeric fields. InvalidCastExceptionFailure of a conversion operation. May be caused by loss of significant digits or an illegal conversion. ArithmeticExceptionA calculation error, such as division by zero. OverflowExceptionOverflow of a variable Dim x As Integer x = 1000000000 IndexOutOfRangeExceptionAn array’s index is out of range. Dim x(5) As Integer x(9) = 25 ExceptionGeneric - any exception
6
Structured Exception Handling 6 Try statements that may cause an error Catch [name As Exception] action statements when an exception occurs [Finally statements that always execute before exiting try block] End Try
7
Structured Exception Handling 7 Private Sub (…) Handles cmdCalculate.Click Try lblResult.Text = txtPrin.Text * txtRate.Text * txtTime.Text Catch ex As Exception msgbox(ex.Message & "|" & ex.GetType.ToString) Exit Sub End Try End Sub This is not as good as the previous example, but it does show that all of the TextBox errors are caught. This is not as good as the previous example, but it does show that all of the TextBox errors are caught. By naming exceptions, you can access their properties and methods. By naming exceptions, you can access their properties and methods. Catches any exception
8
We are going to focus on using exception handling when working on files. 8
9
Structured Exception Handling 9 File Related Exception Classes ExceptionCaused By IO.DirectroyNotFoundExceptionA file within a missing folder is accessed. IO.FileNotFoundExceptionA missing file is accessed. IO.IOExceptionAny file-handling exception including the two mentioned above. For example, an attempt is made to delete or move an open file.
10
Dim inFile As StreamReader Try inFile = File.OpenText(“D:\Dimensions\measures.txt”)...... Catch ex As IODirectoryNotFoundException msgbox(“The requested folder is not on the CD.”) Catch ex As FileNotFoundException msgbox(“The file is not in the specified folder.”) Catch ex As IOException msgbox(“Check to see if there is a CD in drive D:.”) Finally inFile.close() End Try...... 10 Assume D: is a CD drive Exceptions are caught from the most specific to the most general Exceptions are caught from the most specific to the most general
11
Private Sub cmdRead_Click(…) Handles cmdRead.Click Dim inFile As StreamReader inFile = File.OpenText("GBA.txt") lblGB.Text = inFile.ReadToEnd inFile.Close() End Sub 11 Remember, the file has to be in the same directory as the.exe file Remember, the file has to be in the same directory as the.exe file Gettysburg Address Revisited
12
Private Sub cmdRead_Click(…) Handles cmdRead.Click Dim inFile As StreamReader inFile = File.OpenText("GBA.txt") lblGB.Text = inFile.ReadToEnd inFile.Close() End Sub 12 Gettysburg Address Revised Try Catch ex As FileNotFoundException msgbox(“The file is not in the specified folder.”) Exit Sub End Try Let’s examine a far more elegant way to open files using built-in dialog boxes
13
OpenFileDialog Box 13
14
14 Gettysburg Address Re-revised We will use a menu item to display a file dialog box, from which we will select a file
15
Private Sub FileOpenToolStripMenuItem_Click(…) … Dim inFile As StreamReader.ShowDialog() inFile = File.OpenText(MyOpenFileDialog.FileName) txtGB.Text = inFile.ReadToEnd inFile.Close() End Sub 15 An OpenFileDialog property To learn more about OpenFileDialog use HelpHelp To learn more about OpenFileDialog use HelpHelp An OpenFileDialog method which also returns a value indicating which button was pressed An OpenFileDialog method which also returns a value indicating which button was pressed This code creates and displays an OpenFileDialog box with MOST error trapping/exception catching already done! This code creates and displays an OpenFileDialog box with MOST error trapping/exception catching already done! OpenFileDialog Box MyOpenFileDialog
16
Private Sub FileOpenToolStripMenuItem_Click(…)… Dim inFile As StreamReader MyOpenFileDialog.ShowDialog() inFile = File.OpenText(MyOpenFileDialog.FileName) txtGB.Text = inFile.ReadToEnd inFile.Close() End Sub 16 There is at least two errors that the OpenFileDialog box does not handle! There is at least two errors that the OpenFileDialog box does not handle! OpenFileDialog Box Improvements MyOpenFileDialog.InitialDirectory = "C:\Users\Anthony Nowakowski\Documents\Visual Studio 2010\Projects“ MyOpenFileDialog.Filter = "txt files (*.txt)|*.txt|csv files (*.csv)|*.csv"
17
Private Sub FileOpenToolStripMenuItem_Click(…)… Dim inFile As StreamReader inFile = File.OpenText(MyOpenFileDialog.FileName) txtGB.Text = inFile.ReadToEnd inFile.Close() End Sub 17 OpenFileDialog Box Improvements MyOpenFileDialog.InitialDirectory = "C:\Users\Anthony Nowakowski\Documents\Visual Studio 2010\Projects“ MyOpenFileDialog.Filter = "txt files (*.txt)|*.txt|csv files (*.cs)|*.csv" If(MyOpenFileDialog.ShowDialog()= DialogResult.Cancel) Then Exit Sub Else End If Note: this code handles both the Cancel button and the Red X Note: this code handles both the Cancel button and the Red X You could have also tested DialogResult.OK You could have also tested DialogResult.OK
18
18 Other Dialog Boxes SaveFileDialog - MySaveFileDialog.ShowDialog() Also found in the Toolbox under Dialogs ColorDialog - MyColorDialog.ShowDialog() Also found in the Toolbox under Dialogs PrintDialog - MyPrintDialog.ShowDialog() Found in the Toolbox under Printing In addition to the OpenFileDialog box, there are several including: You will be implementing SaveFileDialog in your next assignment You will be implementing SaveFileDialog in your next assignment
19
Read the Description Of Assigment-5 19
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.