ES-26 Exer-4 Prelab
ES-26 Exer-4 Prelab
Error Handling
ENSC 26 Laboratory
REPETITION STRUCTURES
Do Loop Statements
Unlike For-Next which repeats a block of statements
with definite number of times, Do-Loop executes a
block of statements at an indefinite number of times
depending on the Boolean value of a condition.
It has two forms:
A green wavy underline appearing on a code gives warning that the code will compile but may produce
undesirable results during runtime
Note that, the presence of compilation errors PREVENTS program execution.
CATEGORIES OF ERRORS
Three Major Categories of Errors
2. Run Time (or exception) Errors
Run time errors occur when a combination of data and code causes an invalid condition during runtime in what
otherwise appears to be a valid code (e.g. when the user enters a string data for a numeric variable) during
coding.
These happen during program execution, and appears only after you compile and run your code
In the example below, a text value was placed in a numeric variable:
CATEGORIES OF ERRORS
Three Major Categories of Errors
2. Run Time (or exception) Errors
In the example below, if the user enters a value greater than the range of Integer data type, an error will occur.
CATEGORIES OF ERRORS
Three Major Categories of Errors
2. Run Time (or exception) Errors
some external factors include the user, hard disk, database or a network path that is not behaving as expected
CATEGORIES OF ERRORS
Three Major Categories of Errors
3. Logic (or Symantec) Errors
Logic errors are errors that prevent your program from doing what you intended it to do; generally, the hardest
type to fix
a mistake that would cause the program to produce incorrect, unwanted or unexpected results in response to
user actions
These errors are due to poor algorithm, process flow issues or incorrect writing of formula/calculations as shown
below (the formula for sum was written before the variables received the values from the textboxes).
ERROR HANDLING
Exception Class
Exception is an object class in Visual BASIC which contains almost all of the possible run time errors.
There are many types of run time errors that are included in the .Net Exceptions list but the most common are as
follows:
1. InvalidCastException: occurs when a variable receives a value that cannot be converted as part
of its class (i.e. a text value was placed in a numeric variable)
2. OverflowException: occurs when a variable receives a value beyond its range.
3. NotFiniteNumberException: occurs when a variable receives an infinite number (displayed as
“NaN” which stands for “Not a Number”), such as 0/0 calculations.
EXCEPTION HANDLING
Structured Exception Handling
To prevent runtime errors, anticipate their occurrence by providing error handling codes to deal with them,
using the Try… Catch… Finally Statement
Uses a control structure, with each block having one or more associated handlers which specifies some form of
filter condition on the type of exception it handles (order specific).
Try… Catch… Finally statement is used specifically for structured exception handling.
Syntax:
EXCEPTION HANDLING
Structured Exception Handling
Try Clause
Statements placed inside this clause are the error-prone code such as value overflow, wrong memory
address, and other forms of exceptions.
Note that almost all programming code are placed within the TRY clause to capture all possible errors in the
program.
However, placing all the code within a single Try clause is not always desired, especially during debugging
and multiple level algorithms.
EXCEPTION HANDLING
Structured Exception Handling
Catch Clause
The Catch clause defines the error handler. It has two main functions:
1. Filters exceptions: If an exception occurred in the program, all program block under the Try Clause
where the exception occurred will be skipped.
2. Performs the alternative program block: Whenever an exception occur in the program, all the
program block within the catch clause which has a matching exception shall be performed.
Different types of errors can have different catch sections. There can be more than one catch clause.
If no matching Catch clause is found, the program shall be halted, and an error will occur.
EXCEPTION HANDLING
Structured Exception Handling
Catch Clause
Syntax:
ErrObjName – provides a variable name for the active error object (default name is “ex”)
ErrorClass – identifies an exception class
Note that adding the code “Messagebox.Show([ErrObjName].Message)” in the program block in the catch
clause shall display the detailed exception.
EXCEPTION HANDLING
Structured Exception Handling
Catch Clause
EXCEPTION HANDLING
Structured Exception Handling
Finally Clause
The “do this or die” part of the Try block
If an error occurs in the Try statement, the code in the Finally section will always be processed after the
relevant Catch clause is complete.
If no error occurs, the Finally clause will still be processed before leaving the Try statement.
This clause is optional but REQUIRED when no catch clauses are provided.
SUMMARY
• If-Then statements are used in directing the program flow which
requires multiple decision criteria.
• On the other hand, Select Case Statements are used if a single
variable or literal is to be evaluated against decision statements.
• For-Next Loop statements are used in for codes requiring definite
number of repetitions. Otherwise, use Do Loops.
• A routine that uses On Error may call another routine that uses
Try…Catch… Finally with no problems.
SAMPLE PROBLEM
SAMPLE PROBLEM