0% found this document useful (0 votes)
31 views23 pages

ES-26 Exer-4 Prelab

The document discusses different types of repetition structures like Do loops and error handling techniques in programming. It explains Do loops have two forms - Do While and Do Until - and evaluates the loop condition differently. The document also categorizes programming errors into compilation, run time and logic errors and describes using exception handling and Try-Catch-Finally blocks to prevent run time errors from halting program execution.

Uploaded by

ktetsurou19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views23 pages

ES-26 Exer-4 Prelab

The document discusses different types of repetition structures like Do loops and error handling techniques in programming. It explains Do loops have two forms - Do While and Do Until - and evaluates the loop condition differently. The document also categorizes programming errors into compilation, run time and logic errors and describes using exception handling and Try-Catch-Finally blocks to prevent run time errors from halting program execution.

Uploaded by

ktetsurou19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Do Loops and

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:

1. Do While: Repeats a block of code as


long as a certain condition is True.
2. Do Until: Repeats a block of code until
a condition is True
REPETITION STRUCTURES
Do Loop Statements
Pre-test Loops
 Evaluates the condition before processing any of the
instructions within the loop
 Syntax:
REPETITION STRUCTURES
Do Loop Statements
Pre-test Loops
 Example:
REPETITION STRUCTURES
Do Loop Statements
Post-test Loops
 Evaluates the condition after processing the instructions within the loop
 Guarantees that the statements inside the loop is processed at least once.
 Syntax:
REPETITION STRUCTURES
Do Loop Statements
Post-test Loops
 Example:
PROGRAMMING ERRORS
 When computer programs are
written, there will always be a
possibility of writing an
erroneous code known as bugs.
 Bugs make a program perform
differently from the designed
algorithm, or it might not run at
all.
 Hence, when there is an error in
program, it has to be located
and corrected.
 Grace Hopper – programmer of
the early computer Harvard
Mark 1, who discovered the first
computer bug (an actual bug)
PROGRAMMING ERRORS
Locating Errors
 The process of finding and fixing errors in a program is
called debugging.
 Debugging is an iterative process which involves
repeatedly running and writing the program until all
errors are gone.
 In most cases, a program need not be stopped for
debugging through the process of Edit and Continue in
Visual BASIC (program will stop where the error occurred
and may be continued after editing).
CATEGORIES OF ERRORS
Three Major Categories of Errors
1. Compilation Errors
 Compilation errors are errors due to syntax issues such as misspelling, improper syntax or wrong keyword use.
 Code with compilation errors are highlighted with a blue underline (and also listed in the Error List window) as
shown below:

 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

You might also like