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
Printable View
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 loadQuote:
Originally Posted by Shallag
on error goto ____
should work
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.
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 wrote
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 alotQuote:
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.
Shallag:confused:
I think you can just do this...:
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.Code:Sub Main
On Error GoTo Global_Error_Handler
' Open form/do stuff
Exit Sub
Global_Error_Handler:
'handle errors
End Sub
Hope I've made sense
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
yep... thought you could do that... but looks like you can't - sorry shallag...
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
this can be put in a module and can be called from any function within the application..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
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.