SlideShare a Scribd company logo
2
Most read
3
Most read
12
Most read
Error-Handling and Debugging
• errors can be grouped into three categories:
       1. Syntax errors
       2. Run-time errors
       3. Logic errors
• Syntax errors occur when you mistype a command or leave out
  an expected phrase or argument.
• Visual Basic detects these errors as they occur and even
  provides help in correcting them.
• You cannot run a Visual Basic program until all syntax errors
  have been corrected.
• A syntax error (compiler error) is a programming mistake that
  violates the rules of Visual Basic, such as a misspelled
  property or keyword.
Error-Handling and Debugging

• Run-time errors are usually beyond your program's control.
  Examples include:
• when a variable takes on an unexpected value (divide by zero),
  when a drive door is left open, or when a file is not found. Visual
  Basic allows you to trap such errors and make attempts to correct
  them.
• Logic errors are the most difficult to find. With logic errors, the
  program will usually run, but will produce incorrect or unexpected
  results.
• A logic error is a human error — a programming mistake that makes
  the program code produce the wrong results
• The Visual Basic debugger is an aid in detecting logic errors.
•
Error-Handling and Debugging
• 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.
Error-Handling and Debugging
• Run-Time Error Trapping and Handling
• Run-time errors are trappable.
• That is, Visual Basic recognizes an error has occurred
  and enables you to trap it and take corrective action.
• Error trapping is enabled with the On Error statement:
1. On Error GoTo label
2. On Error GoTo 0
3. On Error GoTo line#
4. On Error Resume Next
Error-Handling and Debugging
1. On Error GoTo label
• ddThe Visual Basic On Error GoTo statement is the
  foundation of handling trappable errors.
• When you execute an On Error GoTo Label statement
  in your code, execution is transferred to the code
  starting at Label if a trappable error has occurred.
Error-Handling and Debugging
• The best way to explain how to use error trapping is to look at
  an outline of an example procedure with error trapping.
      Sub SubExample()
      .       . [Declare variables, ...]
• .                   On Error GoTo HandleErrors
• .           . [Procedure code]
• .                          Exit Sub
              HandleErrors:
• .                          [Error handling code]
      End Sub
Error-Handling and Debugging
• Once you have set up the variable declarations, constant
  definitions, and any other procedure preliminaries, the On
  Error statement is executed to enable error trapping.
• Your normal procedure code follows this statement.
• The error handling code goes at the end of the procedure,
  following the HandleErrors statement label.
• This is the code that is executed if an error is encountered
  anywhere in the Sub procedure.
• Note you must exit (with Exit Sub) from the code before
  reaching the HandleErrors line to avoid inadvertent
  execution of the error handling code.
Error-Handling and Debugging
• On Error GoTo 0
• Since the error handling code is in the same procedure
  where an error occurs, all variables in that procedure are
  available for possible corrective action.
• If at some time in your procedure, you want to turn off
  error trapping, that is done with the following
  statement:On Error GoTo 0
• The Err object returns, in its Number property
  (Err.Number), the number associated with the current
  error condition.
• The Error() function takes this error number as its
  argument and returns a string description of the error.
Error-Handling and Debugging
• Once an error has been trapped and some action taken, control must be
  returned to your application.
• That control is returned via the Resume statement. There are three
  options:
• Resume                Lets you retry the operation that caused the
                        error. That is, control is returned to the line
                        where the error occurred. This could be
                        dangerous in that, if the error has not been
                        corrected (via code or by the user), an infinite
                        loop between the error handler and the
                        procedure code may result.
• 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.
Error-Handling and Debugging
• On Error Resume Next
• The On Error Resume Next statement provides an
  easy way to disregard errors, if you want to do so.
• Once you execute this statement, execution continues
  with the next line of code if the current line generates
  an error, and the error is disregarded.
Error-Handling and Debugging
• Debugging In Visual Basic
• Visual Basic offers a powerful suite of debugging options—
  notably the ability to single-step through your code as it’s
  executing.
• The standard way to debug is to place a breakpoint at a
  particular line in your code, and when execution reaches that
  line, it will halt and Visual Basic will enter the Debug state,
  giving you access to your code and variables.
• You can examine the contents of those variables and work
  through your code line by line, watching program execution
  behind the scenes.
• You place a breakpoint in code by moving the text insertion
  caret to that line and either selecting Toggle Breakpoint in the
  Debug menu or pressing F9.
Error-Handling and Debugging
• To move through your program step-by-step, you can
   select these stepping options in the Debug menu:
• Step Into—Single-step through the code, entering
                     called procedures if encountered.
• Step Over —Single-step through the code, stepping
              over procedure calls.
• Step Out— Step out of the current procedure.
Error-Handling and Debugging
• immediate window
• You can print directly to the immediate window while an
  application is running.
• Sometimes, this is all the debugging you may need. A few
  carefully placed print statements can sometimes clear up
  all logic errors, especially in small applications.
• To print to the immediate window, use the Print method:
• print[List of variables separated by commas or semi-
  colons]
• Eg: ?a;b or print a; b
Error-Handling and Debugging
• Locals Window
• The locals window shows the value of any variables
  within the scope of the current procedure.
• As execution switches from procedure to procedure,
  the contents of this window changes to reflect only
  the variables applicable to the current procedure.
• Repeat the above example and notice the values of A
  and B also appear in the locals window.
Error-Handling and Debugging
• Watch Expressions
• The Add Watch option on the Debug menu allows you to
  establish watch expressions for your application.
• Watch expressions can be variable values or logical
  expressions you want to view or test. Values of watch
  expressions are displayed in the watch window.
• In break mode, you can use the Quick Watch button on the
  toolbar to add watch expressions you need.
• Simply put the cursor on the variable or expression you want
  to add to the watch list and click the Quick Watch button.
• Watch expressions can be edited using the Edit Watch option
  on the Debug menu.
Error-Handling and Debugging
Error-Handling and Debugging
Error-Handling and Debugging

More Related Content

What's hot (20)

PPTX
Inheritance in java
RahulAnanda1
 
PPTX
Java Data Types
Spotle.ai
 
PPTX
Java(Polymorphism)
harsh kothari
 
PDF
Arrays in Java
Naz Abdalla
 
PPTX
Classes objects in java
Madishetty Prathibha
 
PPTX
oops concept in java | object oriented programming in java
CPD INDIA
 
PPTX
Exception handling c++
Jayant Dalvi
 
PDF
Java threads
Prabhakaran V M
 
PPS
Java Exception handling
kamal kotecha
 
PPTX
Java program structure
shalinikarunakaran1
 
PPT
9. Input Output in java
Nilesh Dalvi
 
PPTX
Exception Handling in Java
lalithambiga kamaraj
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PPTX
Interface in java
PhD Research Scholar
 
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
PPTX
JAVA AWT
shanmuga rajan
 
PPT
Introduction to method overloading & method overriding in java hdm
Harshal Misalkar
 
PDF
Class and Objects in Java
Spotle.ai
 
PPTX
Exception Handling in object oriented programming using C++
Janki Shah
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
Inheritance in java
RahulAnanda1
 
Java Data Types
Spotle.ai
 
Java(Polymorphism)
harsh kothari
 
Arrays in Java
Naz Abdalla
 
Classes objects in java
Madishetty Prathibha
 
oops concept in java | object oriented programming in java
CPD INDIA
 
Exception handling c++
Jayant Dalvi
 
Java threads
Prabhakaran V M
 
Java Exception handling
kamal kotecha
 
Java program structure
shalinikarunakaran1
 
9. Input Output in java
Nilesh Dalvi
 
Exception Handling in Java
lalithambiga kamaraj
 
Exception Handling in JAVA
SURIT DATTA
 
Interface in java
PhD Research Scholar
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
JAVA AWT
shanmuga rajan
 
Introduction to method overloading & method overriding in java hdm
Harshal Misalkar
 
Class and Objects in Java
Spotle.ai
 
Exception Handling in object oriented programming using C++
Janki Shah
 
Function in C program
Nurul Zakiah Zamri Tan
 

Similar to Error handling and debugging in vb (20)

PPTX
Debugging
nicky_walters
 
PPT
Ppt lesson 06
Linda Bodrie
 
PPT
Ppt lesson 06
Linda Bodrie
 
PPT
Ppt lesson 06
Linda Bodrie
 
PPT
AVB201.2 Microsoft Access VBA Module 2
Dan D'Urso
 
PDF
VISUAL_BASIC_LECTURE_NOTE_A_Z_MADE_EASY.pdf
NALANDACSCCENTRE
 
PPT
VBscript
vikashraj2090
 
PDF
Chapter10
vishalw24
 
PDF
Vba Integrating Python For Enhanced Automation A Comprehensive Guide To Advan...
hedulakhalof
 
PPTX
VISUAL
emi kenye
 
PPT
S D D Program Development Tools
gavhays
 
PPTX
Simple debugging
nicky_walters
 
PPTX
Simple debugging
nicky_walters
 
PPTX
Learn VBA Training & Advance Excel Courses in Delhi
ibinstitute0
 
PDF
Online Advance Excel & VBA Training in India
ibinstitute0
 
PPT
Vb introduction.
sagaroceanic11
 
PPTX
Vba Class Level 1
Ben Miu CIM® FCSI A+
 
PPT
Introduction of exception in vb.net
suraj pandey
 
PPT
Error handling in visual fox pro 9
Mike Feltman
 
PPTX
Chapter 08
llmeade
 
Debugging
nicky_walters
 
Ppt lesson 06
Linda Bodrie
 
Ppt lesson 06
Linda Bodrie
 
Ppt lesson 06
Linda Bodrie
 
AVB201.2 Microsoft Access VBA Module 2
Dan D'Urso
 
VISUAL_BASIC_LECTURE_NOTE_A_Z_MADE_EASY.pdf
NALANDACSCCENTRE
 
VBscript
vikashraj2090
 
Chapter10
vishalw24
 
Vba Integrating Python For Enhanced Automation A Comprehensive Guide To Advan...
hedulakhalof
 
VISUAL
emi kenye
 
S D D Program Development Tools
gavhays
 
Simple debugging
nicky_walters
 
Simple debugging
nicky_walters
 
Learn VBA Training & Advance Excel Courses in Delhi
ibinstitute0
 
Online Advance Excel & VBA Training in India
ibinstitute0
 
Vb introduction.
sagaroceanic11
 
Vba Class Level 1
Ben Miu CIM® FCSI A+
 
Introduction of exception in vb.net
suraj pandey
 
Error handling in visual fox pro 9
Mike Feltman
 
Chapter 08
llmeade
 
Ad

Recently uploaded (20)

PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
epi editorial commitee meeting presentation
MIPLM
 
Controller Request and Response in Odoo18
Celine George
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Ad

Error handling and debugging in vb

  • 1. Error-Handling and Debugging • errors can be grouped into three categories: 1. Syntax errors 2. Run-time errors 3. Logic errors • Syntax errors occur when you mistype a command or leave out an expected phrase or argument. • Visual Basic detects these errors as they occur and even provides help in correcting them. • You cannot run a Visual Basic program until all syntax errors have been corrected. • A syntax error (compiler error) is a programming mistake that violates the rules of Visual Basic, such as a misspelled property or keyword.
  • 2. Error-Handling and Debugging • Run-time errors are usually beyond your program's control. Examples include: • when a variable takes on an unexpected value (divide by zero), when a drive door is left open, or when a file is not found. Visual Basic allows you to trap such errors and make attempts to correct them. • Logic errors are the most difficult to find. With logic errors, the program will usually run, but will produce incorrect or unexpected results. • A logic error is a human error — a programming mistake that makes the program code produce the wrong results • The Visual Basic debugger is an aid in detecting logic errors. •
  • 3. Error-Handling and Debugging • 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.
  • 4. Error-Handling and Debugging • Run-Time Error Trapping and Handling • Run-time errors are trappable. • That is, Visual Basic recognizes an error has occurred and enables you to trap it and take corrective action. • Error trapping is enabled with the On Error statement: 1. On Error GoTo label 2. On Error GoTo 0 3. On Error GoTo line# 4. On Error Resume Next
  • 5. Error-Handling and Debugging 1. On Error GoTo label • ddThe Visual Basic On Error GoTo statement is the foundation of handling trappable errors. • When you execute an On Error GoTo Label statement in your code, execution is transferred to the code starting at Label if a trappable error has occurred.
  • 6. Error-Handling and Debugging • The best way to explain how to use error trapping is to look at an outline of an example procedure with error trapping. Sub SubExample() . . [Declare variables, ...] • . On Error GoTo HandleErrors • . . [Procedure code] • . Exit Sub HandleErrors: • . [Error handling code] End Sub
  • 7. Error-Handling and Debugging • Once you have set up the variable declarations, constant definitions, and any other procedure preliminaries, the On Error statement is executed to enable error trapping. • Your normal procedure code follows this statement. • The error handling code goes at the end of the procedure, following the HandleErrors statement label. • This is the code that is executed if an error is encountered anywhere in the Sub procedure. • Note you must exit (with Exit Sub) from the code before reaching the HandleErrors line to avoid inadvertent execution of the error handling code.
  • 8. Error-Handling and Debugging • On Error GoTo 0 • Since the error handling code is in the same procedure where an error occurs, all variables in that procedure are available for possible corrective action. • If at some time in your procedure, you want to turn off error trapping, that is done with the following statement:On Error GoTo 0 • The Err object returns, in its Number property (Err.Number), the number associated with the current error condition. • The Error() function takes this error number as its argument and returns a string description of the error.
  • 9. Error-Handling and Debugging • Once an error has been trapped and some action taken, control must be returned to your application. • That control is returned via the Resume statement. There are three options: • Resume Lets you retry the operation that caused the error. That is, control is returned to the line where the error occurred. This could be dangerous in that, if the error has not been corrected (via code or by the user), an infinite loop between the error handler and the procedure code may result. • 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.
  • 10. Error-Handling and Debugging • On Error Resume Next • The On Error Resume Next statement provides an easy way to disregard errors, if you want to do so. • Once you execute this statement, execution continues with the next line of code if the current line generates an error, and the error is disregarded.
  • 11. Error-Handling and Debugging • Debugging In Visual Basic • Visual Basic offers a powerful suite of debugging options— notably the ability to single-step through your code as it’s executing. • The standard way to debug is to place a breakpoint at a particular line in your code, and when execution reaches that line, it will halt and Visual Basic will enter the Debug state, giving you access to your code and variables. • You can examine the contents of those variables and work through your code line by line, watching program execution behind the scenes. • You place a breakpoint in code by moving the text insertion caret to that line and either selecting Toggle Breakpoint in the Debug menu or pressing F9.
  • 12. Error-Handling and Debugging • To move through your program step-by-step, you can select these stepping options in the Debug menu: • Step Into—Single-step through the code, entering called procedures if encountered. • Step Over —Single-step through the code, stepping over procedure calls. • Step Out— Step out of the current procedure.
  • 13. Error-Handling and Debugging • immediate window • You can print directly to the immediate window while an application is running. • Sometimes, this is all the debugging you may need. A few carefully placed print statements can sometimes clear up all logic errors, especially in small applications. • To print to the immediate window, use the Print method: • print[List of variables separated by commas or semi- colons] • Eg: ?a;b or print a; b
  • 14. Error-Handling and Debugging • Locals Window • The locals window shows the value of any variables within the scope of the current procedure. • As execution switches from procedure to procedure, the contents of this window changes to reflect only the variables applicable to the current procedure. • Repeat the above example and notice the values of A and B also appear in the locals window.
  • 15. Error-Handling and Debugging • Watch Expressions • The Add Watch option on the Debug menu allows you to establish watch expressions for your application. • Watch expressions can be variable values or logical expressions you want to view or test. Values of watch expressions are displayed in the watch window. • In break mode, you can use the Quick Watch button on the toolbar to add watch expressions you need. • Simply put the cursor on the variable or expression you want to add to the watch list and click the Quick Watch button. • Watch expressions can be edited using the Edit Watch option on the Debug menu.