×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Using Err.Raise question

Using Err.Raise question

Using Err.Raise question

(OP)
Hi

I was reading about Using Err.Raise and I had a question or few that I was holing someone could answer. Here is the article I am referencing: Link

If I were to add Err.Raise to the code, when an error occurs in the section of code, it should go to Pub Cost ERROR_INVALID_DATA. If there is an ON ERROR GOTO eh, that should override the Err.Raise?

Which takes priority over the other? What are the conditions that this occurs? If Err.Raise is before ON ERROR GOTO eh, does Err.Raise default or will ON ERROR GOTO eh override the Err.Raise?
 

CODE

If Len(Sheet1.Range("A1")) <> 5 Then
        Err.Raise ERROR_INVALID_DATA, "ReadWorksheet" _
            , "The value in the cell A1 must have exactly 5 characters."
End If 


CODE

Public Const ERROR_INVALID_DATA As Long = vbObjectError + 513

Sub ReadWorksheet()

    On Error Goto eh
    
    If Len(Sheet1.Range("A1")) <> 5 Then
        Err.Raise ERROR_INVALID_DATA, "ReadWorksheet" _
            , "The value in the cell A1 must have exactly 5 characters."
    End If
    
    ' continue on if cell has valid data
    Dim id As String
    id = Sheet1.Range("A1")
    

Done:
    Exit Sub
eh:
    ' Err.Raise will send code to here
    MsgBox "Error found: " & Err.Description
End Sub 

Thanks,

Mike

RE: Using Err.Raise question

Hi,

Err.Raise does not cause your code to Goto eh. It is the On Error Goto eh along with whatever line of coder caused the error.

Good error handling takes a lot of careful analysis and thought.
https://www.automateexcel.com/vba/error-handling/

Confession: I did very little error handling in my VBA, because almost all was for me as an aid to accomplish my job. When I did encounter an error, (usually data or situational) I fixed the data or modified the situation and went on.

If you are coding for a release to a customer base, you better have tested to the break point, every feature of your application and made it idiot-proof. On those few occasions that I did release to a customer base, I probably spent 3 to 4 times more effort, than just coding a solution, to attempt to make it idiot-proof.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Using Err.Raise question

Consider this trivial example:

CODE -->

Public Sub Example1()
Dim A As Integer
Dim B As Integer

A = 1
B = 0

Debug.Print A / B

End Sub 

Divide by zero error and your program stops ungracefully.

Add a simple error handler:

CODE -->

Public Sub Example2()
Dim A As Integer
Dim B As Integer

On Error GoTo eh

A = 1
B = 0

Debug.Print A / B

Exit Sub

eh:
MsgBox "Oops! " & Err.Number & " " & Err.Description

' do something else useful here

End Sub 

The same division by zero error happens, but this time the program exits gracefully under your control.
In my trivial example2, the control is just a message box, but by replacing my comment 'do something useful here with something useful allows the program to continue, the way you want it to in this case.

As a side effect, we've also learned that the Err.Number for division by zero is 11.

You can see a list of the predefined by VBA errors here: https://onlinelibrary.wiley.com/doi/pdf/10.1002/97...

Notice that there is no predefined error for Len(Sheet1.Range("A1")) <> 5

Step things up a bit more and correct some syntax errors in the example from your original link and we get something very similar to the original example.

CODE -->

Public Sub Example3()
Dim A As Integer
Dim B As Integer


On Error GoTo eh

A = 1
B = 0

If B = 0 Then
    Err.Raise Number:=vbObjectError + 1051, Description:="Why did you set B = 0?  You know that will break your code!"
End If

Debug.Print A / B

Exit Sub

eh:
MsgBox "Oops! " & Err.Number - vbObjectError & " " & Err.Description

' do something else useful here

End Sub 


We have defined our very own error. It has Err.Number = 1051 and Err.Description = "Why did you set B = 0? You know that will break your code!"
In Example 3 I've replaced the default division by zero error with my own.

But Err.Raise gives you the ability to define any error condition that you want. In Example3 the condition is If B = 0.

Your fundamental confusion is this:

Quote (remeng)

when an error occurs in the section of code, it should go to Pub Cost ERROR_INVALID_DATA. If there is an ON ERROR GOTO eh, that should override the Err.Raise?

Err.Raise is not an alternative to On Error GoTo.

Err.Raise gives you the ability to create an error wherever you might need one, for whatever condition you define for On Error GoTo to catch and work with.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close