1

small question, in Visual Basic 6.0 (VB6)

Assuming I missed a possible situation and the user did something and did not expect, then he gets an error that crashes my application. Is there an event I can work with that will execute a certain code if that happens ?

Tried Form_Terminate \ Unload \ Query_Unload, not much luck there.

Crash: AKA Run-Time Error.

Stavm
  • 7,833
  • 5
  • 44
  • 68
  • I assume there is no entry in the Eventlog from the crash? – rene Sep 03 '14 at 15:37
  • Maybe look into http://msdn.microsoft.com/en-us/library/windows/hardware/dn641144.aspx if no other option is feasible. – rene Sep 03 '14 at 15:39
  • Crash = Run Time Error. Rene, i already know how to handle errors, im talking about the larger scale, not specific capturing. (i can always place an 'on error goto X', or god forbid, 'on error resume next' (aka tape the check-engine light') but what if that error came from another piece of code, should i scan for errors and place that on every event i code on ? – Stavm Sep 03 '14 at 15:42
  • There are many software out there that for example, on unintended crash, pop up a 'report this error to developers' form, without, in my opinion, having to place an 'On error go to X' on every event triggered. – Stavm Sep 03 '14 at 15:49
  • @polisha989 Does the runtime log an event when it crashes? – rene Sep 03 '14 at 15:49
  • @rene Yes, but again, im looking for a larger scale, not specific ones, i would like to, like Robert Harvey said, Bulletproof it, without having to invest hundreds of code lines just for that, it kinda misses the point in Rapid Development Programming if i can't achieve that – Stavm Sep 03 '14 at 15:53
  • You could run an scheduled task that responds to that event to inform you that something bad happened, maybe even collect a dump. Or see if WER can help you with the totally unforseen crash scenario. That are all options that can work today so more rapid than that is not posible... And Robert said it **CAN NOT** be done which I agree with. – rene Sep 03 '14 at 15:59
  • We wrote little add-in to VB6 environment, which instrumented *every* method/sub/function/event with custom error handling (including creating run-time callstack, user action history and simialr valuable info). There are no other bullet-proof methods than add error handling to *all* code. In addition, we hacked into system exception handling and could check/log/notify/report about system errors (were once called GPFs) too. Only things left unhandled were blue screens :) – Arvo Sep 04 '14 at 08:50
  • You could write some kind of container application which starts your application and then monitors the running processes to see if it's still running ... of course this assumes that the container application doesn't crash :) – Hrqls Sep 08 '14 at 12:37

2 Answers2

2

You can add an On Error check at any level, including your Sub Main. Any errors that are not caught by an error check at that functions level will rise through the call stack to the main/initial method where you could there catch them.

You could then gracefully display the error (so you or your users are aware of it) and then resume whatever method is best at that point. And, remember, you can do this at several different strategic levels and locations.

If the errors occur in an event procedure, then these won't be trapped in Sub Main() so you will also need to catch them there.

Any errors that rise to the top of the stack, either out of Sub Main() or an event procedure will be caught by the runtime and are fatal. Your code will get no notification of this.

You may also be interested in the post Good Patterns For VBA Error Handling.

Community
  • 1
  • 1
LimaNightHawk
  • 6,613
  • 3
  • 41
  • 60
0

On the the frequent ways to capture error in VB6, is by using the On Error and the GOTO tags. At the beginning of each Function, you just declare the goto in case of error, and whenever an error arises, the GOTO part will be executed.

Note that the Goto part should go at the end of the method, so as after running the codes, no other piece of code is executed but leave the Function. Check the example below;

Private Function MyMethod()

  Dim sMsg As String

  On Error Goto ErrHandler

  ' ...code here...

  Exit Function

ErrHandler:
sMsg = "Error #" & Err.Number & ": '" & Err.Description & "' from '" & Err.Source & "'"
GoLogTheError sMsg

End Function
Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61