473,387 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Err.Number is always = 5

I have several routines that use the Try-Catch-EndTry method. When an
error occurs in one of these routines, I display it like this:

Catch ex As Exception
MsgBox("Error #" & Err.Number & ": " & Err.Description)
Exit Function
End Try

For some reason, no matter what the error is, the Err.Number is ALWAYS
"5". I have generated different errors and while the Description
changes, the error number never does.

I am sure I am missing something simple & I would appreciate any
suggestions.

Thanx!

Nov 21 '05 #1
6 16554
Use the properties of the Exception object, ex. Your code is querying
the Err object which may not be reliable for all exceptions.

Err.Number = 5 means "Access is denied."

Nov 21 '05 #2
Thanks a lot for your quick response.

I tried to use the properties of the Exception object, but I could not
find one that shows a unique identifier (like an error number). I
would like to have this number so I can handle different errors in
different ways. For example, if the error code equalled "5"
(indicating an access error), I could show a more "user friendly"
message in addition to the generic/technical one.

Specifically, which property would contain this? I tried
InnerException, Source, GetBaseException, and TargetSite, but none of
these was what I was looking for.

On a related note, can anyone direct me to a listing of the Err.Number
descriptions?

Thanx again!

Nov 21 '05 #3

"Kirk" <lo****@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Thanks a lot for your quick response.

I tried to use the properties of the Exception object, but I could not
find one that shows a unique identifier (like an error number). I
would like to have this number so I can handle different errors in
different ways. For example, if the error code equalled "5"
(indicating an access error), I could show a more "user friendly"
message in addition to the generic/technical one.

Specifically, which property would contain this? I tried
InnerException, Source, GetBaseException, and TargetSite, but none of
these was what I was looking for.

On a related note, can anyone direct me to a listing of the Err.Number
descriptions?

Thanx again!


Generally to do that you would catch multiple exception types, rather than a
single generic exception type

Nov 21 '05 #4
Kirk,

I don't believe exceptions have a specific number associated with them
(although the exception's HResult property might be useful).

Instead, you can catch specific exceptions and supply your own user-friendly
message. Here is an example that catches various exceptions and supplies an
application-specific message:

Catch ex As InvalidCastException
MsgBox("Input values must be numbers", MsgBoxStyle.Exclamation,
"Input Error")

Catch ex As DivideByZeroException
MsgBox("The number of donors cannot be zero",
MsgBoxStyle.Exclamation, "Input Error")

Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")

End Try

Kerry Moorman
"Kirk" wrote:
Thanks a lot for your quick response.

I tried to use the properties of the Exception object, but I could not
find one that shows a unique identifier (like an error number). I
would like to have this number so I can handle different errors in
different ways. For example, if the error code equalled "5"
(indicating an access error), I could show a more "user friendly"
message in addition to the generic/technical one.

Specifically, which property would contain this? I tried
InnerException, Source, GetBaseException, and TargetSite, but none of
these was what I was looking for.

On a related note, can anyone direct me to a listing of the Err.Number
descriptions?

Thanx again!

Nov 21 '05 #5
Kerry,

Thanks for your detailed response! Your example code worked well for
me. I guess I was still stuck in the "old school" mindset of capturing
errors by identifying the unique error numbers. Also, I did not really
understand how the exception Catch terminology worked.

Thanks again.

Nov 21 '05 #6
"Kirk" <lo****@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I tried to use the properties of the Exception object, but I could not
find one that shows a unique identifier (like an error number).


That's because you don't [iften] need one. The *Type* of the
Exception itself tells you what went wrng, which is why you tend to
see Catch blocks like this

Try
SomethingDodgy()
Catch ex as ReallySpecificTypeOfException
Recover()
Catch ex as SomeOtherTypeOfException
Giveup()
Catch ex as EvenMoreGenericException
Throw

' or, possibly, even
Catch ex as System.Exception
If TypeOf ex Is ArgumentException Then
OhDearYouDidSomethingWrong()
Else
Throw
End If
End Try

Notice that your Catch'es have to be "in order", catching the
"smallest" Exception first, i.e. the one "most" derived from
System.Exception. Check the Class hierarchy for each Exception
in MSDN; the /fewer/ levels it is away from System.Exception, the
earlier you should code to catch it. Beware:

Try
Catch ex as Exception
' /Always/ arrives here
Catch ex as ArgumentException
' *Never* arrives here
End Try

BTW, *some* Exceptions, like the COMException, /do/ have a
numeric ErrorCode property (which, inthis case, holds the errant
HResult), but you only get these thrown by methods that "call out"
to COM. Managed Code doesn't do this so no error "number" is
necessary.

HTH,
Phill W.
Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: chuck | last post by:
Hello, I am trying to figure out how to get a number (always an int) from a string. so if i have something like <input type="text" id="product13" name="product13" onblur="return...
0
by: Hooligan | last post by:
I have this ancient application that was originally created in VB4 and has been upgraded and upgraded. The application is massive and uses the Err object and the owners will not pay for a rewrite...
2
by: fzmaster | last post by:
Actually i need to know how to detect if a number is a perfect square. But i'v been thinking.. if i sqrt(x) and check if this result has mantissa... so it is not a interger...and surely not a...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
3
by: cj | last post by:
Is there a function or method that would round a long number (always up) to the next integer?
22
by: Kd | last post by:
I have the following set up to give me aresponse number R05-001 I would like it to reset to R06-001 at new year ResponseNo: "R" & Right(Format(Date(),"yyyy"),2) & "-" & Format(,"000") This is...
9
by: L33VaNcL33F | last post by:
I like to make a javascript that generate random number from the range number within : (10016486 and 99999985). the number always + 22423 that begin from 10016486 and end at 99999985 Example...
5
by: le0 | last post by:
Hello guys, Im really having a hard time doing this, I have a record set with the ItemNo field with the data type as Text. In the record that I have, I want to find the missing number in the...
16
by: Petrakid | last post by:
Hey, I have a task to complete. I am trying to figure out the best way, in C++ to determine the following. There is this farm with pigs and chickens. Only the legs of the pigs and chickens look...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.