CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2004
    Posts
    23

    Question On Error for a project

    Hi All,

    Is there anyway that you can create on error statement that will run over on entire project...form..module... I do not want to have go back through alot of modules and add on error code.

    Hoping someone can help me with this

  2. #2
    Join Date
    Dec 2002
    Location
    NC
    Posts
    125

    Re: On Error for a project

    Quote Originally Posted by Shallag
    Hi All,

    Is there anyway that you can create on error statement that will run over on entire project...form..module... I do not want to have go back through alot of modules and add on error code.

    Hoping someone can help me with this
    I could be wrong but I would think that on form load
    on error goto ____
    should work
    R.L.T.W. A+, NET+, CCNA

    doin' my best

  3. #3
    Join Date
    Jul 2004
    Posts
    23

    Question Re: On Error for a project

    I wrote a short test program that has inbuit overflow error if you input a number greater that 32000, I put the on error in form load and does seem to trap..will attach code, mayby I am putting in wrong place.
    Attached Files Attached Files

  4. #4
    Join Date
    Oct 2001
    Location
    Melbourne, Australia
    Posts
    576

    Re: On Error for a project

    I would have your program start from a "sub main" (in a module), put the global error handler in there, and open your startup form manually.

  5. #5
    Join Date
    Jul 2004
    Posts
    23

    Question Re: On Error for a project

    Zeb wrote

    I would have your program start from a "sub main" (in a module), put the global error handler in there, and open your startup form manually.
    Zeb...Thank you for your reply what I want is global error handling just not sure how to set it up...most of my programs run form sub main...that was just a test..if you you could please give me an example of how to set global error handing I would appreciate it alot


    Shallag

  6. #6
    Join Date
    Oct 2001
    Location
    Melbourne, Australia
    Posts
    576

    Re: On Error for a project

    I think you can just do this...:
    Code:
    Sub Main
        On Error GoTo Global_Error_Handler
    
        ' Open form/do stuff 
    
        Exit Sub
    
    Global_Error_Handler:
    
        'handle errors
    
    End Sub
    Now, I think the best way to do that would be to make an error handler class object that does that which is declared and instantiated globally. Then you could (if you wanted) have a public property of that class which could contain the state of your program (i.e. any information regarding what section the code is currently in, etc) and set within the bulk of your code. Then, the error handler class could handle generic errors, but also be able to customise (i.e. numeric overflow errors are fine in some places, but need to be handled differently in your invoice totals calculations (for example)). After the "Global_Error_Handler" label, you could then just call the error handlers "handle" (example) method - which could maybe return true or false, depending on whether the error is critical or execution can resume.

    Hope I've made sense

  7. #7
    Join Date
    Jul 2004
    Posts
    23

    Question Re: On Error for a project

    Zeb


    I think I understand where you are going...tried insert the error handling glabally frim sub main (and made sub main the start up object)..however still having trouble, will attach code can you please have a quick look and give some pointers...

    Regards

    Shallag
    Attached Files Attached Files

  8. #8
    Join Date
    Oct 2001
    Location
    Melbourne, Australia
    Posts
    576

    Re: On Error for a project

    yep... thought you could do that... but looks like you can't - sorry shallag...

  9. #9
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: On Error for a project

    Well VB 6.0 doesn't support an application wide error handler, like the one which you are asking for. You will have to write the error handling routine for all the events/procedures.

    However, you can write an application wide routine to which you can pass the error number and that routine will contain the code for all the errors that you want to handle..

    Try something like this
    Code:
    Option Explicit
    'This Enum is used to return value back
    'to the calling function from the Error Handler
    Public Enum returnErr
    	retry
    	resumeNext
    	errorNotRecognized
    	errorNonRecoverable
    End Enum
    'Function : handleError
    'Input Parameters : Err Number
    'Return Value : One of the values from the Enum Defined above
    'Description : Checks the error number and displays a specific message
    ' checks what was user's response and returns back to the calling function
    Public Function handleError(errNumber As Long) As returnErr
    	'The message that will be displayed to the user
    	Dim errMsg As String
    	'The buttons that are to be displayed
    	Dim msgType As Integer
    	'the response that we get from the user
    	Dim buttonSelected As Integer
    	'check what type of error has occured
    	Select Case errNumber
    		Case 53	'File Not Found
    			errMsg = "This File does not exist"
    			msgType = vbExclamation + vbOKOnly
    		Case 71	'Disk Not Ready
    			errMsg = "Disk is not ready!! Do you want to retry?"
    			msgType = vbExclamation + vbRetryCancel
    		Case 61	'Disk Full
    			errMsg = "Disk is Full"
    			msgType = vbCritical + vbOKOnly
    		Case Else  'Some other error has occured which cannot be recognized
    			errMsg = "Unexpected Behaviour!!" & vbCr + Err.Description
    			msgType = vbInformation + vbOKOnly
    	End Select
    	'show the message and check which button was clicked
    	buttonSelected = MsgBox(errMsg, msgType, App.Title)
    	'Depending on the response return the value to the calling function
    	Select Case buttonSelected
    		Case 1, 2, 5	  'Ok, Cancel or ignore Button has been selected
    			handleError = errorNonRecoverable
    		Case 4			 'Retry Button has been selected
    			handleError = retry
    		'other code goes here for other buttons
    	End Select
    End Function
    this can be put in a module and can be called from any function within the application..
    only thing you have to write in the procedures where you want to handle the errors is......
    Code:
    Public Sub myProc()
       On Error Goto errH
       'Some line which has caused the error
       Exit Sub
     
    errH:
    	Select Case handleError(Err.Number)
    		Case retry
    			Resume
    		Case resumeNext
    			Resume Next
    		Case errorNotRecognized
    			Exit Sub
    		Case errorNonRecoverable
    			Exit Sub
    	End Select
    End Sub

    MSDN Ref:
    The Visual Basic ON ERROR statement is designed to encapsulate error handling for each procedure or module in an application. This behavior is by design, and is intended to conform with object oriented programming (OOP) conventions. When you create an application, you may wish to have a generic error handler that traps all errors for the application, and handle exceptions to this routine as they occur in various modules.

    The ON ERROR statement does not allow the use of an application wide ON ERROR routine. However, it is possible to create a generic routine that is called from within each procedure or method in the application.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured