Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you handle errors in error handlers in VB6?

I frequently encounter this situation in my VB6 applications

Private Sub DoSomething

  On Error Goto err1

  Call ProcessLargeBatch1
  Call ProcessLargeBatch2
  '... more ...'

  Exit Sub

err1:
  Call Cleanup 'Specific for DoSomething'
  Call HandleError 'General error handling: Logging, message box, ...'

End Sub

The Cleanup procedure sometimes reverts actions, rolls back a transaction, deletes temporary files, and so on. In most cases this operation can also fail.

What do I do in this case? I'd add an On Error Resume Next into the error handler but that deletes the existing Err object. Adding an error handler to Cleanup has the same problem.

What is the best way to ensure that the original errors still gets processed/logged?

EDIT: One additional problem is that I also want to notify the user of the error. Sometimes it is important, that the cleanup happens fast and I don't want the message box block the application for a long time and do the cleanup after the user acknowledges the error.

like image 330
Daniel Rikowski Avatar asked Dec 31 '22 05:12

Daniel Rikowski


2 Answers

Firstly, read all the information out of the Err object that you will need, i.e. number, description, etc., then clear the error and do what you want.

Change the way you inform the user to use the values you have cached, and not to use the Err object itself.

like image 98
Patrick McDonald Avatar answered Jan 04 '23 15:01

Patrick McDonald


From your example you are doing the cleanup properly. Your HandleError should only log the error not do any UI. The UI is handled up at the form level.

What you need to when an error occurs is

  1. Clean Up
  2. Log the Error
  3. Raise the Error Again via Err.Raise

This will work it's way up the call stack to the event that called the original code. Then the sequence will become

  1. Clean Up
  2. Log the Error
  3. Display the the Error Notification Dialog

Note that your Error Logging can be intelligent in that subsequent logs of the same error can just add to the recorded call stack.

You want to make sure that EVERY event has a error handler. Not every procedures needs one but definitely every event. Unhandled errors in a event will cause a VB6 application to shut down unexpectedly.

like image 41
RS Conley Avatar answered Jan 04 '23 15:01

RS Conley