Title: Advanced Interactive Programming Objectoriented Programming Error Handling
1Advanced Interactive ProgrammingObject-oriented
ProgrammingError Handling
2Outline
- Collection
- Group discussions on OOP
- Discussion on OOP concepts
- Learning from each other
- Error handling
- Compile errors
- Run time errors
- Logic errors
3Collection
4Collection Concept
- A way of grouping a set of related items
- A collection usually consists of a group of
objects of different types, known as its elements - Example collections include set, array and list
- Some are ordered and others unordered
- Iteration and enumeration utility classes
- Provide mechanisms to walk through all objects in
a collection - Provide methods to manipulate elements of a
collection
5Forms and Controls Collections
- VB provides default form and control collection
objects for managing and accessing their elements - Collection members have index values
- These collections are 0-based, i.e. the index
value starts from 0 - Collections have a Count property
6Example Forms Collections
5 - 1
7The Generic Collection Class
- Unlike the default Forms and Controls collections
- Used for grouping programmers own objects
- Generic collections are 1-based
- The generic collection has a single property
Count - It must first be declared and bound to a variable
name
8Generic Collection Methods
- The Add method is used to add members to the
collection - The Remove method is used to remove members from
the collection - The Item method is used to return single members
of the collection when furnished an index value
9A Generic Collection Example
Set Queue New Collection Set Account New
AccountCls Account.AssignOwner (Fred) Queue.Add
(Account) Set Account New AccountCls Account.Ass
ignOwner (Mary) Queue.Add (Account) Print
Queue.Count 2 Print Queue(1).Owner Fred Print
Queue(2).Owner Mary
10Collection in OOP
- Very common because we need to manage a set of
objects they might all have the same class or
derived from different subclasses of the same
superclass. - More functionalities, for example, performing
type or existence testing when adding an object
or retrieving objects in terms of other
attributes keys, the textual index. - Extensive use of Iterator or Enumerator class to
manipulate individual objects
11References
- See MSDN library
- Topic Form Object, Forms Collection and Controls
Collection - Collection Object in Visual Basic
- Using Properties and Collections to Create Object
Models
12Seminar and Discussion on Object Oriented
Programming
13Schedule
- Grouping
- Group discussions
- Question answering and explanation if necessary
- Approx. 30 minutes
14Topic One
- Explain and discuss what you understand by the
following concepts with your group member. It
should include their relationships, usage in VB,
benefits, etc. - Object
- Class
- Interface
- Class libraries
15Topic Two
- Explain and discuss what you understand by the
following concepts with your group member. It
should include their relationships, usage in VB,
benefits, etc. - Encapsulation
- Inheritance
- Class hierarchy
- Implementation inheritance
- Interface inheritance
- Polymorphism
- Implementation inheritance based polymorphism
- Interface inheritance based polymorphism
16Topic Three
- Explain and discuss what you understand by the
following concepts with your group members. - Procedural programming
- Visual programming
- Event-oriented programming
- Object-oriented programming
- Are they all the same or different thing?
17 18Error Types
- Compile (including syntax) errors, which result
from incorrectly constructed code. - Such as incorrect usage of keywords, punctuation,
terms, conventions, operators, control
structures, etc. - Runtime errors, which occur while the application
is running when a statement attempts an operation
that is impossible to carry out. - Statements are syntactically correct
- Must run before being detected
- Logic errors, which occur when an application
doesn't perform the way it was intended.
19Error Handling Purposes
- Make programs robust and fault-tolerant
- Recover from fatal errors to avoid consequences
- Terminate programs gracefully if not possible
to recover - Different strategies are used to tackle different
types of errors - Syntax checker and compiler for compile errors
- Error handling statements for run-time errors
- Debugging tools for logic errors
5 - 1
20Handling Runtime Errors
21Using On Error Statement
- Turns the error trapping process on
- Alerts Visual Basic to watch for any run-time
errors in the code listed below the On Error
statement - If an error occurs, the On Error statement sends
program control to the error-handling routine - Turns the error trapping process off
- Nested On Error statements
22On Error Statement Syntax
- On Error GoTo line
- Line is either a line label or a line number that
identifies the position of the procedures error
handler - Line label or number must be in the same
procedure - The error handler is the set of instructions that
tells the application how to handle the trapped
errors - On Error GoTo 0
- Disable an error trap
- Raised error are still detected but not directed
to the error handler
23Procedure Structure with On Error Trapping
- Private Sub statement
- Dim statements, etc.
- On Error GoTo line
- statements
- Exit Sub
- line
- error-handling instructions
- End Sub
24Error Handling Example
5 - 2
25The Err Object
- An intrinsic object with global scope. There is
no need to create an instance of it in your code. - When a run-time error occurs, the properties of
the Err object are filled with information that
uniquely identifies the error and information
that can be used to handle it. - The properties of the Err object are set by the
generator of an error Visual Basic, an object,
or the programmer. - The Err object's properties are reset to zero or
zero-length strings ("") after an Exit Sub, Exit
Function, Exit Property or Resume Next statement
within an error-handling routine
26The Err Object
27Internal Runtime Error Codes
Code Message 3 Return without
GoSub 5 Invalid procedure call 6 Overflow 7
Out of memory 9 Subscript out of
range 10 This array is fixed or temporarily
locked 11 Division by zero 13 Type mismatch
28Using the Err Object
Different errors
5 - 3
Different code
Detailed information
29Resume Statement
- Used to specify where in a procedure, function or
class execution continues after an error is
handled - Provides three
- choices
On Error Goto label statements Exit Sub /
Function label error handling code
somelabel
An error raised here
Resume Next statement
Resume statement
Resume somelabel statement
30Call Stack
- A call stack is a list of procedures that were
called in order to get to the current procedures -
31Error Handling in a Call Stack
- Error handling become complex when multiple calls
are made - Programmers must decide which procedure handles
which errors - An error trap and an error handler must be used
in pairs. If a error is raised but no error
handler, a fatal error happens and the program
terminate disgracefully
32Rethrowing Errors
- Errors can be rethrowed (reraised) after being
trapped - Many reasons, mainly different strategies for
different errors - Using the Raise method of the err object
33Rethrowing Error Example
34Handling Logic Errors
35Preventing Errors
- Following conventions - prefixes
- Use comments and explicit variable declarations
- Use the syntax checker
- Proper scoping
- etc.
36Break Mode
- VB IDE operates in different modes
- Break mode, design mode and run mode
- Break mode for debugging purpose
- Execution suspended
- Variable values and program logic can be checked
- Edit code and continue execution
37Break Mode
- Many ways to start break mode
- Stop statement in code
- Debug object Assert method
- Automatically start the Immediate Window
38Using Immediate Window
- Start Immediate Window, click the Immediate
Window item in the View Menu at design mode - View debugging output while the program is
running.
39Immediate Window
- Evaluate any expression, variable, or object and
view the value that is returned - Query the value of a variable or a property while
running an application. While execution is
halted, assign or change the variable a new value
as you would in code. - Call and execute procedures
40Immediate Window Example
41The Debug Object
- Debug object Assert method for testing conditions
- e.g. Debug.Assert(xgt0 And xlt9)
- Use the Print method of Debug to display the
values of expressions and variables in the
Immediate Window - Testing purpose only, will not be compiled in the
executable
42Alternative Debugging Strategies
- Trace debugging using Print or MsgBox
statements to trace and verify the execution flow - Log files using Write or Print statements
- Problems
- Manually evaluating and checking expressions and
values of programs in Immediate Window is not
scalable and manageable for complex applications
43The VB Debugger
- Debugging tools cannot diagnose or fix errors,
but help analyze - How execution flows from one part of the
procedure to another - How variables and property settings change as
statements are executed - A debugger help you look inside your application
to determine what happens, what and where
something went wrong
44Debugging Tools in VB Debugger
Debug Tool Bar Debug Menu
45Using Step Into, Over, etc.
- Select the Step Into/Over in Debug menu to step
through code one statement or one procedure at a
time - Display variable values by positioning mouse
pointer over the variable - Use Debug.print to print variable or property
values in the Immediate Window
46Step through Bank Example
47Working with Break Points
- Defines a line in the Code window where Visual
Basic suspends execution of the application - Setting a break point by selecting a line and the
Toggle Breakpoint item in the Debug menu or
directly clicking margin indicator bar - Break Points remain in your code until you clear
them - To clear a break point, select it and press F9 or
the Toggle Break Point button - Clear all Break Points by pressing CtrlShiftF9
48Example
49Locals Window
- Select Locals Window in View menu to start the
Locals Window - Displays the current value and data type of all
local variables - You can inspect or change these values at Break
Points
50Locals Window Example
51Watch Window
- Add either at design time or in break mode
- Monitor both local and module variables
- Watch different behaviour
Use the Add Window
Use Drag and drop
52Quick Watch
- Highlight an expression or a variable
- Select Quick Watch in Debug menu
- Click Add to add the expression to Watch Window
53Debugger and Error Handlers
- Hard to debug code when error handlers are
involved - Disable error handlers using Break on All Errors
in the General tab - Any run time error also places IDE in break mode
54Summary
- Collection
- OOP seminar
- Error handling