Handle Error.

  • Thread starter Cactus [ÏÉÈËÇò]
  • Start date
C

Cactus [ÏÉÈËÇò]

A procedure often Number Overflow.
So I put this handler in procedure.


Private Sub Hint()

On Error GoTo Overflow
....... ' Some codes.
Exit Sub

Overflow:
If (Err = 6) Then
......' Handling error.
End If

End Sub


but if other Error happen.
it could be a unhandle error.
because I not handing all err number.
 
H

Harald Staff

Hi

I suggest a Select Case structure on err number, like this:

Sub test()
Dim i As Long

On Error GoTo ErrHandler

i = 23232323232323# ^ 2
MsgBox "i=" & i

i = 23 / 0
MsgBox "i=" & i

Exit Sub

ErrHandler:
Select Case Err.Number
Case 6
MsgBox "Overflow"
i = 500
Resume Next
Case 11
MsgBox "Div 0"
i = 0
Resume Next
Case Else
MsgBox Error, Err.Number
End Select
End Sub

You may find a list of errors useful when you work on this. Run this on a
blank worksheet:

Sub ErrorList()
Dim L As Long
Dim R As Long
On Error Resume Next
For L = 1 To 1000
Err.Raise L
If Error <> "Application-defined or object-defined error" Then
R = R + 1
Cells(R, 1).Value = Err.Number
Cells(R, 2).Value = Error
End If
Err.Clear
Next
End Sub


HTH. Best wishes Harald
 
C

Cactus [ÏÉÈËÇò]

Harald


Well, that Err Number List is great.
Can I using this way?
Throws Err to default handler.


Overflow:
If (Err = 6) Then
.....' Handling error.
Else
Err.Raise Err
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top