Failing to Catch Exception
Hi folks,
I have a WinForm app, written in MS Video Studio .NET C++ 2003, that examines folders in the systems main drive (ie. C:\). Whenever it comes across the "System Volume Information" file, access is denied and the program presents a dialog window indicating the following:
"An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll. Additional information: Access to the path "c:\System Volume Information\" is denied."
I've tried catching this exception, but can't seem to do it. So far I've tried:
catch (IOException* e) { }catch (System::UnauthorizedAccessException* e) { }But neither seems to handle the exception. What exceptionshould I be trying to catch?
[971 byte] By [
Zero] at [2007-12-23]
I'm actually surprised why it doesnt catch it under the UnauthorizedAccessException;
Are you calling this code from another try{} catch{} block?
Didn'twe already do this? Perhaps it is now time you post your code so
we can help you find the "right spot" to put your exception handler.
Hi,
Put on your catch also:
catch (System::Exception* e)
This will catch any exception that is inherited from Exception, once you do this, run the code again and check
the exception type to confirm.
Regards
Salva - that is technically bad practice and more expensive catching a general exception than a specific exception.
I *think* that the developer could be calling the method from another try {} catch {} block, which if an exception is thrown from the method called, it will throw it back to the caller and the caller method does not have the appropriate exception to catch in place.
but lets wait and see
The problem was that the code throwing the exception was itself not within a try block.

It came right before another try block, which is where I originally thought the problem was.
I've been looking through the .NET doc's to try and find a method that will allow me to determine if I have access to a given folder - so that I can entirely avoid the system throwing the above exception.
When the exception above is thrown, it ends the thread and the file count comes to an early end. I tried adding a __finally{...} block which contains the remaining code of the thread, but it didn't seem to help, the count still ended early.
Using the old fashioned, "dir c:\*.* /s" , I find that my system has about 153,000 files. But, when my app encounters the "c:\System Volume Information" folder, the thread stops counting at about 140,000 files.
Here's what the code looks like:
void ProcessFolder(String* strFolder) {String* strFiles[];
try { strFiles = Directory::GetFiles(strFolder); }catch (UnauthorizedAccessException* e) { /* error processing here */ }__finally {String* dirs[] = Directory::GetDirectories(strFolder, S"*");
UInt32 iTotalFolders = dirs->Length;
for (UInt32 iIndex = 0; iIndex < iTotalFolders; iIndex++) ProcessFolder(dirs[iIndex]);
}
}
What I'd like to do is first check to see if I can access the folder - if not, then I'll just skip over it to continue processing the next folder. Any ideas how this can be done?
I've never done VC++.NET but here is a guideline which i hope helps
basically during your iteration through each folder, inside of this, place a try {} catch (unauthorizedaccess) {} block, this will then still iterate through the folders but just catching the unauthorised folder exception.
This should be ok.
That doesn't seem to be working. I've tried a few variations as well, but all seem to stop at the same point - about 13K files short of my count in the DOS console. Here are examples of what I tried...
example1 {
try { strFiles = Directory::GetFiles(strFolder); }
catch (UnauthorizedAccessException* e) {}
}
example 2 {
strFiles = Directory::GetFiles(strFolder);try {} catch (UnauthorizedAccessException* e) {}
}
example 3 { // This code did not compile since "UnauthorizedAccess" was not a recognized identifier
try { strFiles = Directory::GetFiles(strFolder); } catch (UnauthorizedAccess* e) {}}
*sigh* Any other ideas? Or am I back to testing to see if I have access?
it should be unauthorizedaccessexception.
So hang on, when you do:
strFiles = Directory::GetFiles(strFolder);
is this where the exception is then thrown? (when you give it C:\ as the strFolder)
sorry, my mind is fried at the moment
I tried doing that (C#) and it didnt throw anything but now im guessing you are just going through each folder and file correct? and the exception is thrown when it goes to the System Information folder?
this should be the work around, here is a pseudo:
foreach directory in theDirectories
try {
foreach file in directory
//do stuff with file
end foreach
}
catch (UnauthorizedAccessException* e) { .. }
end foreach
the same thing applies for a recursive folder method, just replace the foreach file... with foreach directory (in the psuedo)
Hi ahmedilyas
It is not bad practice at all, you can catch your specific exceptions and last one you should catch the general exception, otherwise your client applicaiton will see how the application crashes. Your code should handle all the exceptions and be solid. Expect the unexpected.
Cheers
Iagree with Salvador, in this particular case you definitely want to
catch Exception and not one of its derived specializations. It is
also most definitely not slower...
I guess we have to agree to disagree! :-) but lets not get into this as it is going a bit Off topic.
I am wondering if the solution has now been given?
Try this: public static int CountFiles(String folder) {
try {
DirectoryInfo dir = new DirectoryInfo(folder);
int cnt = dir.GetFiles().Length;
foreach (DirectoryInfo d in dir.GetDirectories()) {
try {
if
((d.Attributes & FileAttributes.System) == 0) cnt +=
CountFiles(d.FullName);
} catch (Exception) { }
}
return cnt;
}
catch (Exception) {
return 0;
}
}
Beware that CountFiles sees hidden files too, dir /s *.* doesn't...
Thnx nobugz, putting the main code into the try block while catching the general exception type worked nicely. The function was able to count all files on the drive
