473,788 Members | 2,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to check which IO.Exception it was?

Hi there!
I am developing an Windows Explorer-like application that will tell you the
size of any folder.
But so far I have come across some problems.
For instance, you may not have access to all folders in NTFS drives, or a
floppy disk may not be in the drive when I access it.
The question is, how can I check in a Try Catch statement the precise error
that was returned?
Isn't there an error number? I could check the ex.Message but I believe that
isn't the best way to do it...
How can I check the error number or something?

Thank you!
Nov 21 '05 #1
2 1908
* "André Nogueira" <an**@netcabo.p t.NOSPAM> scripsit:
I am developing an Windows Explorer-like application that will tell you the
size of any folder.
But so far I have come across some problems.
For instance, you may not have access to all folders in NTFS drives, or a
floppy disk may not be in the drive when I access it.
The question is, how can I check in a Try Catch statement the precise error
that was returned?
Isn't there an error number? I could check the ex.Message but I believe that
isn't the best way to do it...
How can I check the error number or something?


Take a look at the exception's 'HResult'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2
André,
Have you tried checking for one of the exception classes that inherit from
IOException?

System.Object
System.Exceptio n
System.SystemEx ception
System.IO.IOExc eption
System.IO.Direc toryNotFoundExc eption
System.IO.EndOf StreamException
System.IO.FileL oadException
System.IO.FileN otFoundExceptio n
System.IO.PathT ooLongException

Remember that most exceptions have a specific Exception class that you can
check for.

Try
' do something
Catch ex As DirectoryNotFou ndException
' do something because the directory was not found
Catch ex As FileNotFoundExc eption
' do something because the file was not found
Catch ex As PathTooLongExce ption
' do something because the path was too long
Catch ex As IOException
' do something because a different IO exception occurred
Catch ex As Exception
' generally I would not catch Exception here
' rather I let my global exception handlers deal with
System.Exceptio n
' included more to show proper order of catching exceptions...
End Try

When you catch exceptions as above, be certain to catch the more specific
(most derived) exceptions first, and catch more general (base) exceptions
last.

NOTE: not having access to an NTFS drive may be a security exception instead
of an IO exception, I have not tested it to see which is raised. I either
check my exception log (that my global exception handler wrote) or I
temporarily add a "Catch ex As Exception" and look at what exception was
caught. Instead of temporarily adding a "Catch ex As Exception", the
debugger can break on the exception so you can identify it.

Hope this helps
Jay

"André Nogueira" <an**@netcabo.p t.NOSPAM> wrote in message
news:Og******** ******@TK2MSFTN GP10.phx.gbl...
Hi there!
I am developing an Windows Explorer-like application that will tell you the size of any folder.
But so far I have come across some problems.
For instance, you may not have access to all folders in NTFS drives, or a
floppy disk may not be in the drive when I access it.
The question is, how can I check in a Try Catch statement the precise error that was returned?
Isn't there an error number? I could check the ex.Message but I believe that isn't the best way to do it...
How can I check the error number or something?

Thank you!

Nov 21 '05 #3

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

Similar topics

1
3821
by: martinnitram | last post by:
Dear all, following are some piece of my code (mainly create a socket connection to server and loop to receive data): # function to create and return socket def connect(): server_config = ('192.168.1.50', 3333); sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect()
15
6987
by: Dan S | last post by:
My application asks the user to enter in a date - in the mm/dd/yyyy format. Is there any quick and easy way to verify the date the user enters is formatted correctly? Right now I'm calling DateTime.Parse() and catching the FormatException but it seems this is a bit inefficient - catching the exception that is. There is some pretty obvious delay while it traces back up the call stack. Is there a better way? Something that returns a bool...
7
13992
by: zjut | last post by:
I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : integral-digits]exponential-digits]) and the index is the location after "." in name. First, i should check the given name is a valid float-point, and then, to check the number in name in index locaion is > 0 or not? eg:
6
2215
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time and resource consuming Try... Catch, which in iterative processing is totally unacceptable. -tom
5
20280
by: cmay | last post by:
The only examples I have seen on how to check if a file is locked is to try to open it a catch an exception. MS has stated that you should never use error trapping in this manner. Is there no other way to identify if a file is locked w/o trapping an exception?
19
108025
Frinavale
by: Frinavale | last post by:
Filtering user input is extremely important for web programming. If input is left unfiltered users can input malicious code that can cripple your website. This article will explain how to make sure that the user only submits a number to your .NET web application and also demonstrate how to add JavaScript to your ASPX pages. Upon popular demand, I have added a section that also covers how to use a validator control to check if a text box...
15
3070
by: Brian Simmons | last post by:
Is there an easy way to check to see if a file is in use? I want to open a CSV file from my .NET2 program, and write to it. However, the file could be in use by another program. If that is the case, I don't want to attempt to write to it, just abort. Thanks, Brian
0
2194
by: johnkamal | last post by:
Hi, Some times while opening a page, I am getting the following error message, Please help me to rectify the problem. Server Error in '/' Application. General network error. Check your network documentation. Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception...
4
199
by: sherifffruitfly | last post by:
Try to do something with it, and look at the exception? Some other way? Thanks!
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10370
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...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10113
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
9969
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
5402
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
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2896
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.