25
Errors & Error Trapping

Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

Embed Size (px)

Citation preview

Page 1: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

Errors &

Error Trapping

Page 2: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

Types of Errors

•Syntax - violate language rules

•Semantic - violate usage rules of language

•Run-time – occur while program is running

•Logic – incorrect algorithm

Page 3: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

Syntax Errors

E.g.If X > 0 And < 10 Then

E.g.me.Cls()

Trapped by CompilerMust be corrected before program will compile

Page 4: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 5: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 6: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 7: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 8: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 9: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 10: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 11: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

Run-time Errors (exceptions)

E.g.Dim Value As Single

Value = txtValue.Text

Page 12: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

Run-time Errors (exceptions)

E.g.Dim Value As Single

Value = txtValue.Text

Program will crash if txtValue is empty

Page 13: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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)

Page 14: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 15: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 16: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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 <=

Page 17: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 18: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 19: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 20: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 21: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 22: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 23: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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

Page 24: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

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 ?

Page 25: Errors & Error Trapping. Types of Errors Syntax - violate language rules Semantic - violate usage rules of language Run-time – occur while program is

Use these ideas in your code

Do not let your software crash