Upload
hilary-chase
View
231
Download
4
Tags:
Embed Size (px)
Citation preview
Errors &
Error Trapping
Types of Errors
•Syntax - violate language rules
•Semantic - violate usage rules of language
•Run-time – occur while program is running
•Logic – incorrect algorithm
Syntax Errors
E.g.If X > 0 And < 10 Then
E.g.me.Cls()
Trapped by CompilerMust be corrected before program will compile
Syntax Errors
E.g.If X > 0 And < 10 Then
E.g.me.Cls()
Trapped by CompilerMust be corrected before program will compile
Incorrectspecification oflogical expression
Misspelled name
Syntax Errors
E.g. Dim x As String
For x = 1 To 10Statements to be repeated 10 times
Next x
Trapped by CompilerMust be corrected before program will compile
Syntax Errors
E.g. Dim x As String
For x = 1 To 10Statements to be repeated 10 times
Next x
Trapped by CompilerMust be corrected before program will compile
For loop controlvariable must beinteger or long
Semantic Errors
E.g. Dim x As Integer, Start As Integer, Finish As Integer
Start = 10Finish = 0For x = Start To Finish
Statements to be repeated 10 timesNext x
Trapped by testing
Semantic Errors
E.g. Dim x As Integer, Start As Integer, Finish As Integer
Start = 10Finish = 0For x = Start To Finish
Statements to be repeated 10 timesNext x
Trapped by testing
Start must be < Finishelse, loop will not run
Run-time Errors (exceptions)
E.g.DC = File.OpenText(“Myfile.text”)
E.g. Dim FileName As String
FileName = txtFileName.TextDC = File.OpenText(FileName)
Trapped by run-time system
Run-time Errors (exceptions)
E.g.DC = File.OpenText(“Myfile.text”)
E.g. Dim FileName As String
FileName = txtFileName.TextDC = File.OpenText(FileName)
Trapped by run-time system
Bad extension
Non-existent pathor filename
Run-time Errors (exceptions)
E.g.Dim Value As Single
Value = txtValue.Text
Run-time Errors (exceptions)
E.g.Dim Value As Single
Value = txtValue.Text
Program will crash if txtValue is empty
Logic ErrorsE.g. to calculate the average of First and Second
Average = First + Second / 2
Program will run but produce incorrect results
Trapped only by testing (recommended)or users noticing incorrect results (unreliable)
Logic Errors
E.g.Average = First + Second / 2
Program will run but produce incorrect results
Trapped only by testing or users noticing incorrect results
Correct statement isAverage = (First + Second) / 2
Logic ErrorsE.g.
Function IsCapital (Letter As Char) BooleanIf Letter > “A” And Letter < “Z” ThenReturn True
ElseReturn False
End IfEnd Function
Program will run but produce incorrect resultsTrapped only by testing or users noticing incorrect results
Logic ErrorsE.g.
Function IsCapital (Letter As Char) BooleanIf Letter > “A” And Letter < “Z” ThenReturn True
ElseReturn False
End IfEnd Function
Program will run but produce incorrect resultsTrapped only by testing or users noticing incorrect results
It will return Falsefor A and Z
Use >= and <=
Logic ErrorsE.g.
Function IsLetter ( Ascii As Char) BooleanIf (Ascii >= “A” And Ascii <= “Z”) And _
(Ascii >= “a” And Ascii <= “z”) ThenReturn True
ElseReturn False
End IfEnd Function
Program will run but produce incorrect resultsTrapped only by testing or users noticing incorrect results
Logic ErrorsE.g.
Function IsLetter ( Ascii As Char) BooleanIf (Ascii >= “A” And Ascii <= “Z”) And _
(Ascii >= “a” And Ascii <= “z”) ThenReturn True
ElseReturn False
End IfEnd Function
Program will run but produce incorrect resultsTrapped only by testing or users noticing incorrect results
Use Or
How to Avoid Software Crashes?i.e. how to catch exceptions (or run-time errors)?
Use validation code
Dim Value As SingleIf txtValue.Text = “” Then
Value = 0Else
Value = txtValue.TextEnd If
Your software will NOT crash
How to Avoid Software Crashes?i.e. how to catch exceptions (or run-time errors)?
Use Try-Catch blockTry
One or more statements that may causeRun-time errors (or exceptions)
Catch variable As exceptionStatements to execute when this exception occurs
Catch variable As exceptionStatements to execute when this exception occurs
End Try
Exception keyword To catch
ArithmeticException errors in a calculation
IO.DirectoryNotFoundException an invalid folder path
IO.FileNotFoundException a non-existent file
FormatException bad parameter values
Exception any error
E.g.Sub OpenInputFileDim DC As StreamReader, FileName As String
TryFileName = txtFilename.TextDC = File.OpenText(FileName)
Catch MyFileError As IO.FileNotFoundExceptionMessageBox.Show (“File not found: “ & FileName)
End Try
End Sub
E.g.Sub DoSomething
TryStatements in proceduredoing something
Catch AnyError As Exception
MessageBox (AnyError.Message)
End TryEnd Sub
Will catch anyerror & showits error message
Your software will NOT crash
E.g.Sub DoSomething
TryStatements in proceduredoing something
Catch ThisError As ?
Catch ThatError As ?
Catch OtherError As ?
Catch AnyError As ?
End TryEnd Sub
You may have as manyCatch statements asyou need, using correctKeyword in place of ?
Use these ideas in your code
Do not let your software crash