Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 9th, 2005, 11:41 AM
Authorized User
 
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default handling all types of errors

Hello -

I want to know if there is a better to handle potential error incurring from any subroutins / function?

The macro I have written has many sub and functions, as well as 5 user forms. I have tested all I can, but not enough to catch all possible errors once deployed. How should I handle all type of erros? Is there a sample of codes somewhere that I can learn from?

Thanks.

Kathy

 
Old March 9th, 2005, 12:44 PM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sub mySubroutine()
dim myErrorNo as integer
on error goto myerror

myErrorNo = 1
'put code here..........
myErrorNo = 2
'put code here..........
myErrorNo = 3
'put code here..........
myErrorNo = 4
'put code here..........
myErrorNo = 5
'put code here..........
myErrorNo = 6
'put code here..........

myExit:
exit sub


myError:
msgbox(myerrorNo & ":" & $Error)
goto myexit
End sub


this will display the error that has occured and a line number allowing you to track where the error occured.

cheers

Matthew



 
Old March 9th, 2005, 01:06 PM
Authorized User
 
Join Date: Jun 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Kathy--

  There is no simple way to handle all of the possible errors. Error coding in fact can take up 1/3 or more of your code. Below is the template I use for my procedures. Note that errors are detected in the main body of the procedure and then sent to the error handler at the end. If the procedure does not have an error handler, it will bubble up the call tree until it hits the first routine that does have an error handler. You will have to decide for yourself what happens when the error hits the handler. In my case, I use the "Report" sub in a global instance of SystemError class, an error handler class that will log the error, send a copy to the debug window and report it to a message box. If I only wanted to log and send it to debug, I would call SystemError.post. You do not need such a complicated mechanism. You may just want to use debug.print and/or message box in the error handling section. I use a class to provide uniform processing of all errors and to allow for much more complicated behavior.

  Once you get into the error handler, you have to decide what to do next. Do you abort (End) or do you raise a new error to bubble the error up (SystemError bubbles it up). Remember that in general, error processing should be rare and therefore does not have to be efficient.

Hope this helps.

Barry

ps CLASS_NAME and Class_ProcedurePath are declared once in every class and bas file (though in a base file CLASS_NAME would be called BAS_NAME.

''================================================ =============================
Private Const CLASS_NAME = "PutClassNameHere"
''================================================ =============================
Private Function Class_ProcedurePath(ByVal ProcedureName As String) As String
  Class_ProcedurePath = CLASS_NAME & ">" & ProcedureName
End Function
''================================================ =============================
Private Sub dummy()
'------------------------------------------------------------------------------
  Const PROCEDURE_NAME = "dummy"
  Const BAD_xxx = 32601
  On Error GoTo ErrorHandler
'------------------------------------------------------------------------------
 Dim variable As Variant

   If Not variable Then
     Err.Raise BAD_xxx, , "Bad Enum <" & variable & ">"
   End If

Exit Sub' MUST BE HERE, OTHERWISE EXECUTION WILL CONTINUE INTO HANDLER
'------------------------------------------------------------------------------
ErrorHandler:

  Select Case Err.number
    Case BAD_xxx
      SystemError.Report Err.number, Class_ProcedurePath(PROCEDURE_NAME), Err.Description
    Case Else
      SystemError.Report Err.number, Class_ProcedurePath(PROCEDURE_NAME), Err.Description
  End Select

Exit Sub
'------------------------------------------------------------------------------
End Sub






Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 15 Try It Out, Handling Global Errors, p.583 VictorVictor BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 February 26th, 2006 10:50 PM
handling script errors using vb web browser state Beginning VB 6 3 January 19th, 2006 09:01 AM
Can't get errors to display with <html:errors> michaeldill JSP Basics 0 August 2nd, 2004 01:47 PM
Errors Errors DB Errors Ljhopkins VS.NET 2002/2003 0 July 15th, 2003 12:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.