473,902 Members | 4,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16671
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, GetBaseExceptio n, 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.goo glegroups.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, GetBaseExceptio n, 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 InvalidCastExce ption
MsgBox("Input values must be numbers", MsgBoxStyle.Exc lamation,
"Input Error")

Catch ex As DivideByZeroExc eption
MsgBox("The number of donors cannot be zero",
MsgBoxStyle.Exc lamation, "Input Error")

Catch ex As Exception
MsgBox(ex.Messa ge, MsgBoxStyle.Cri tical, "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, GetBaseExceptio n, 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.goo glegroups.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 ReallySpecificT ypeOfException
Recover()
Catch ex as SomeOtherTypeOf Exception
Giveup()
Catch ex as EvenMoreGeneric Exception
Throw

' or, possibly, even
Catch ex as System.Exceptio n
If TypeOf ex Is ArgumentExcepti on Then
OhDearYouDidSom ethingWrong()
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.Exceptio n. Check the Class hierarchy for each Exception
in MSDN; the /fewer/ levels it is away from System.Exceptio n, the
earlier you should code to catch it. Beware:

Try
Catch ex as Exception
' /Always/ arrives here
Catch ex as ArgumentExcepti on
' *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
28825
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 somefunc(this);" /> then have a function somefunc(element){
0
935
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 of the error handling (why would they it works). With new code additions I use try-catch-err.raise and although it works I'm uncomfortable with mixing err object and exceptions. try ......
2
3009
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 perfect square. Well my question is... how can i verify if a number has mantissa??? i'm thx full for R help. bye. fzmaster
10
4602
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 zero-length string (always returns a zero-length string when used in a query expression)" **** How many records are there in FirstTable in which Product Is Null. SELECT COUNT(*) AS CountofNullProdcut
3
20705
by: cj | last post by:
Is there a function or method that would round a long number (always up) to the next integer?
22
5644
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 an expression in a query any help is apprecieated Ken
9
16775
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 random result from generate button : 10016486 + 22423 = 10038909 10038909 + 22423 = 10061332 and so on, maximum to 99999985 or if somebody can convert below c++ code to working javascript/html random number generator. #include <cstdlib> #include...
5
2451
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 series for example: 1 3 4.1
16
4476
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 exactly the same, so for a short person to determine how many possible pigs and how many possible chickens, all they can do is count the total legs of the two. - Chickens seem to always run around in groups of 3 - Pigs seem to always clump...
28
5963
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly. See section 4.7 for Rounding issues.
0
11279
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10981
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9675
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5893
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4725
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3323
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.