Grade 10 Computer Lesson 10 Error Handling

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

Error Handling

Competency
Run unit tests and debugging tools in Visual Studio

Learning Target:
I can recognize syntax, logic and run-time errors and use
applicable debugging tools.
Errors in Visual Basic
• Syntax Error – a violation or error due to wrong spelling, symbols, spacing, and
other factors that make a code unacceptable in VB.
• Needs to be corrected first for the VB to run properly.

• Logic Error – a program bug that causes the application to produce an incorrect or
undesired output.
• Needs to do test data run and check if result is as expected.

• Runtime Error – detected during the execution of the program or when the program
is running. Missing files or links not available are examples of runtimes errors.
Syntax Errors
• Errors such as Syntax Errors
must be fixed.
• Syntax Errors are sometimes
called a compile error.
Syntax Errors
• Occurs when you violated a rule of VB language;
• For example incorrect punctuation, incorrectly referenced
item, or a misspelled word.
Syntax Errors
Syntax Errors

1. VB’s Editor will detect and correct many.


2. A blue squiggly line appears under the code that the
editor does not understand.
3. Mouse over the code to receive more details.
4. The error List window shows errors and more
details.
Logic Errors
• Produce incorrect results. This kind of error is difficult
to find.
Logic Errors
Logic Errors
1. Check calculations and other logic against known
test data results.
2. VB editor cannot detect logic errors.
3. Use Debugging tools to detect the error.
Logic Errors
Runtime Errors
• Occurs while your program
runs.
• When your program tries to do
an operation that is impossible
to carry out.
• The operation violates the
rules of the computer.
Runtime Errors
Possible Causes
• Divide by 0
• Forgetting to initialize a variable.
• File being accessed is not available.
• Wrong File name.
• Wrong File Path.
• Control being referenced is not
declared
Runtime Errors
Runtime Errors
What happens?
• Your program stops running
• Dialog box displays in the Code Editor window.
- description of the error is given
- troubleshooting tips are given
• The program goes into "Break" mode where you can debug it.
Recognizing Errors
You can view errors in your program by going
to the Menu Bar, View and click the Error List
(Ctrl+W+E)

You’ll be able to see it below the Form Design


Window.
Run-Time Error Trapping and Handling
Some ways to minimize errors:
• Design your application carefully.
• More design time means less debugging time.
• Use comments where applicable to help you remember what you
were trying to do.
• Use consistent and meaningful naming conventions for your variables,
objects and procedures.
Run-Time Error Trapping and Handling

Using GoTo Syntax


Run-Time Error Trapping and Handling
1. On Error GoTo label
On Error GoTo statement is the foundation of
handling trappable errors.
When executing On Error GoTo label statement in
your code, execution is transferred to the code
starting Label if a trappable error has occurred.
Run-Time Error Trapping and Handling
1. On Error GoTo label
Run-Time Error Trapping and Handling
1. On Error GoTo label
Run-Time Error Trapping and Handling
2. On Error GoTo 0
• All variables in the procedure are available for possible
corrective action.
• To turn off error trapping, use On Error GoTo 0
• The Err object (Err.Number) returns the number
associated with the current error condition.
• Error() function takes the error number then returns a
string description of the error.
Run-Time Error Trapping and Handling
2. On Error GoTo 0
• Once error has been trapped and some action
taken, control must be returned to your
application.
• The control is returned via Resume statement.
Run-Time Error Trapping and Handling
2. On Error GoTo 0
• Three options for Resume statement:
• Resume – Lets you retry the operation that caused the
error.
• Resume Next – Program control is returned to the line
immediately following the line where the error occurred.
• Resume Label – Program control is returned to the line
labeled label.
Run-Time Error Trapping and Handling
2. On Error GoTo 0
Run-Time Error Trapping and Handling
3. On Error GoTo line #

• Enables the error-handling routine that starts at


line specified in the required line argument.
• The line argument is any line label or line
number.
Run-Time Error Trapping and Handling
3. On Error GoTo line #

• If a run-time error occurs, control branches to


line, making the error handler active.
• The specified line must be in the same
procedure as the On Error statement; otherwise,
a compile-time error occurs.
Run-Time Error Trapping and Handling
4. On Error Resume Next
• Provides an easy way to disregard errors, if you want to do
so.
• Execution continues with the next line of code if the current
line generates an error, and the error is disregarded.
• On Error Resume Next allows your code to continue running
even if an error occurs. Resume Next does not fix an error, it
just ignores it.
Run-Time Error Trapping and Handling
4. On Error Resume Next
• Provides an easy way to disregard errors, if you want to do
so.
• Execution continues with the next line of code if the current
line generates an error, and the error is disregarded.
• On Error Resume Next allows your code to continue running
even if an error occurs. Resume Next does not fix an error, it
just ignores it.
Run-Time Error Trapping and Handling
4. On Error Resume Next
Let's practice...
Identification
1. A violation or error due to wrong spelling, symbols, spacing,
and other factors that make a code unacceptable in VB.
2. Needs to be corrected first for the VB to run properly.
3. A program bug that causes the application to produce an
incorrect or undesired output.
4. Needs to do test data run and check if result is as expected.
5. Detected during the execution of the program or when the
program is running.
Let’s answer!
1. A violation or error due to wrong spelling, symbols, spacing, and
other factors that make a code unacceptable in VB. Syntax Error
2. Needs to be corrected first for the VB to run properly. Syntax
Error
3. A program bug that causes the application to produce an incorrect
or undesired output. Logic Error
4. Needs to do test data run and check if result is as expected. Logic
Error
5. Detected during the execution of the program or when the
program is running. Runtime Error
Today I learned how to:
Recognize syntax, logic and run-time errors and use
applicable debugging tools.
Offline Activities:
• Do worksheet #10
• Answer quiz #10
• Read in advance notes #11

You might also like