Results 1 to 6 of 6

Thread: What error number to choose for a user-defined run-time error?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    762

    What error number to choose for a user-defined run-time error?

    If I need to raise a run-time error that I myself declare (user-defined run-time error), what error number do I allocate to it?
    Code:
       Err.Raise ...
    What is the acceptable range of numbers from within which I can choose a number for this purpose?
    Thanks.

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: What error number to choose for a user-defined run-time error?

    Quote Originally Posted by MSDN
    Visual Basic errors (both Visual Basic-defined and user-defined errors) are in the range 065535. The range 0512 is reserved for system errors; the range 51365535 is available for user-defined errors. When setting the Number property to your own error code in a class module, you add your error code number to the vbObjectError constant. For example, to generate the error number 513, assign vbObjectError + 513 to the Number property.
    https://msdn.microsoft.com/en-us/lib...(v=vs.60).aspx



  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    762

    Re: What error number to choose for a user-defined run-time error?

    Thanks for the help

    There is a piece of code on this page:
    https://www.daniweb.com/programming/...-bin-using-vb6
    that I found a while back and copied to my project.
    This piece of code is to move a file to recycle bin.
    I copied it to my project and it has been working perfectly for me.
    Today, I just happened to notice that this code contains a line:
    Code:
      Err.Raise 52
    This line has never actually executed since I have been using this code, because this code has always managed to successfully send the file to the recycle bin.
    But, should I maintain this line in there as is?
    Or should I change it to
    Code:
      Err.Raise vbObjectError Or 52
    Or (because the range 1-512 is reserved, use another number like 513 and the following code:
    Code:
      Err.Raise vbObjectError Or 513
    Please advise.
    Thanks.

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: What error number to choose for a user-defined run-time error?

    Quote Originally Posted by IliaPreston View Post
    Or should I change it to
    Code:
      Err.Raise vbObjectError Or 52
    Or (because the range 1-512 is reserved, use another number like 513 and the following code:
    Code:
      Err.Raise vbObjectError Or 513
    Please advise.
    Thanks.
    Never!

    See this trappable errors list https://msdn.microsoft.com/en-us/lib...or=-2147217396

    Err 52 refer to "Bad file name or number" error, if you want that code popup this error, pass non exist file to Recycle2

    The Recycle2 was built very bad, it should add error trap and raise the actual error occured.

    If i'm going to use that code, i will change it to
    vb Code:
    1. Private Sub Recycle2(ByVal FQFileOrFolder As String)
    2.     'Late bound.
    3.     Dim Delim As Integer
    4.     Dim FItem As Object
    5.     Const ssfBITBUCKET = 10
    6.     On Error GoTo IO_Err
    7.    
    8.     Delim = InStrRev(FQFileOrFolder, "\")
    9.     With CreateObject("Shell.Application")
    10.         For Each FItem In .NameSpace(Left$(FQFileOrFolder, Delim - 1)).Items
    11.             If Not FItem.IsLink Then
    12.                 If UCase$(FItem.Name) = UCase$(Mid$(FQFileOrFolder, Delim + 1)) Then
    13.                     .NameSpace(ssfBITBUCKET).MoveHere FItem
    14.                 End If
    15.             End If
    16.         Next
    17.     End With
    18.     Exit Sub
    19. IO_Err:
    20.     ' just raise the error occured
    21.     Err.Raise Err.Number
    22.    
    23. '     ' OR add some check for Err.Number
    24. '    If Err.Number = 52 Then
    25. '        'Do something to fix or inform the user about the error
    26. '    ElseIf Err.Number = 53 Then
    27. '        'Do something to fix or inform the user about the error
    28. '        '.
    29. '        '.
    30. '        '.
    31. '        'etc...
    32. '    End If
    33. End Sub



  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    762

    Re: What error number to choose for a user-defined run-time error?

    Quote Originally Posted by 4x2y View Post
    Never!

    ...

    If i'm going to use that code, i will change it to
    vb Code:
    1. Private Sub Recycle2(ByVal FQFileOrFolder As String)
    2.     'Late bound.
    3.     Dim Delim As Integer
    4.     Dim FItem As Object
    5.     Const ssfBITBUCKET = 10
    6.     On Error GoTo IO_Err
    7.    
    8.     Delim = InStrRev(FQFileOrFolder, "\")
    9.     With CreateObject("Shell.Application")
    10.         For Each FItem In .NameSpace(Left$(FQFileOrFolder, Delim - 1)).Items
    11.             If Not FItem.IsLink Then
    12.                 If UCase$(FItem.Name) = UCase$(Mid$(FQFileOrFolder, Delim + 1)) Then
    13.                     .NameSpace(ssfBITBUCKET).MoveHere FItem
    14.                 End If
    15.             End If
    16.         Next
    17.     End With
    18.     Exit Sub
    19. IO_Err:
    20.     ' just raise the error occured
    21.     Err.Raise Err.Number
    22.    
    23. '     ' OR add some check for Err.Number
    24. '    If Err.Number = 52 Then
    25. '        'Do something to fix or inform the user about the error
    26. '    ElseIf Err.Number = 53 Then
    27. '        'Do something to fix or inform the user about the error
    28. '        '.
    29. '        '.
    30. '        '.
    31. '        'etc...
    32. '    End If
    33. End Sub
    This new code that you have provided in here does not raise any error even when it should.
    For example if a nonexistent file name is passed to this Recycle2 procedure, this procedure doesn't do anything and exits in the end without throwing any error!

  6. #6
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: What error number to choose for a user-defined run-time error?

    Quote Originally Posted by IliaPreston View Post
    This new code that you have provided in here does not raise any error even when it should.
    For example if a nonexistent file name is passed to this Recycle2 procedure, this procedure doesn't do anything and exits in the end without throwing any error!
    That means you have On Error Resume Next (or so) in the procedure that call Recycle2, please test it in new project



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width