• Welcome to Valhalla Legends Archive.
 

index out of bounds

Started by Acid~, February 10, 2003, 03:25 PM

Previous topic - Next topic

Mesiah / haiseM

#15
yes thats what i was saying, if you try to set a value some place where it cannot be set, or use an invalid syntax or anything that cant really be skipped, you will get a runtime error, and it will crash, but reading data will not cause a crash if you trap it.
]HighBrow Innovations
Coming soon...

AIM Online Status: 

KBL_BlackIce

Yeah, I was half asleep when I wrote that little code.  I mixed up Option Explicit and On Error  

/me is an idiot
lol

Anyway, yeah, you can put On Error Resume Next ANYWHERE that happens to be IN A CODE BLOCK.  Code block meaning a routine, such as Sub Main() or Form_Load()

When that routine is called, the On Error statement, if I remember correctly, will have priority over any prior On Error statements until the called routine with the On Error statement terminates.

What I mean by this is, if you call Sub Main() which has an On Error statement in it, and in that routine you call Sub Main2() which has On Error in it, and in Sub Main2() you call Sub Main3() which does NOT have On Error, the Main2() routines On Error will be called on any error... I think.

Anyway, it is better to have an On Error Goto ErrHandler type statement, that will call a routine to handle any errors.  

Sub Main()
  On Error Goto ErrHandler

  Err.Raise -1, "Source is MyForm", "ERROR"

  Exit Sub

ErrHandler:
  Call HandleError(Err.Number, Err.Description)
End Sub

Sub HandleError(errNum as Long, errDesc as String)
  'More code here
End Sub

Or something to that effect.


Grok

#17
I don't understand.  Could you say that again, slower?

Yoni

#18
In case I made it unclear, reading *will* crash if you read from an address that's not in a valid context. It won't crash, but will give meaningless data, if you read from an address that's in a valid context (i.e. your process heap or thread stack) but not initialized.

Quote:ErrHandler
Btw, as similar as they may sometimes be, this is VB, not batch. ;)

KBL_BlackIce

lol, oops

ErrHandler:

I make that mistake the most, I swear it. lol


Grok: The original Sub Main() is Sub Form_Load(). To see what I mean in action, simply cut and paste this code into a VB6 project form code, and run it.

You will see a msgbox with "-1 Main2 RAISED ERROR", even through the error raised was given a source of "Main3"

Note that if you use Err.Source, you will see "Main3"

Private Sub Form_Load()
  On Error GoTo ErrHandle1
  Call Main2

  Exit Sub

ErrHandle1:
  Call ErrHandler(Err.Number, "Main1", Err.Description)
End Sub

Sub Main2()
  On Error GoTo ErrHandle2

  Call Main3
  Exit Sub

ErrHandle2:
  Call ErrHandler(Err.Number, "Main2", Err.Description)

End Sub

Sub Main3()
  Err.Raise -1, "Main3", "RAISED ERROR"

End Sub

Sub ErrHandler(num As Long, source As String, desc As String)
  MsgBox num & " " & source & " " & desc
End Sub

if you remove the on error statement and the handler in Main2(), you will get a msgbox with Main1 as the source, which is what I was trying to explain.