trevor1940
asked on
C#: Catching and handling Path Too Long Exception
Hi
Following on from here
Within
if the file is a *.url / shortcut to web page I get a "PathTooLongException " I haven't seen it with other files and it doesn't happen every time
I'm 100% sure The length of the path dose not exceed Int16.MaxValue (32,767) characters.
So how do I handle this exception
I tried something like this but the exception is still raised
Following on from here
Within
......
string[] Files = Directory.GetFiles(folder);
foreach (string file in Files)
{
FileInfo FI = new FileInfo(file); // exception raised
// Do Stuff with FI
}
if the file is a *.url / shortcut to web page I get a "PathTooLongException " I haven't seen it with other files and it doesn't happen every time
I'm 100% sure The length of the path dose not exceed Int16.MaxValue (32,767) characters.
So how do I handle this exception
I tried something like this but the exception is still raised
......
string[] Files = Directory.GetFiles(folder);
foreach (string file in Files)
{
try
{
FileInfo FI = new FileInfo(file); // exception is still raised
// Do Stuff with FI
}
catch
{
//Do nothing
continue;
}
}
ASKER
OK I'll give it a go I thought by not specifying the exception it would capture all exceptions?
BTW I won't do nothing but will create a error log
BTW I won't do nothing but will create a error log
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi
I moved the "try" thus
This worked How might I capture the Exception reason and print this to the ErrorLog ?
I moved the "try" thus
try
{
string[] Files = Directory.GetFiles(folder);
int FilesCount = Files.Length;
if (FilesCount >=1)
{
foreach (string file in Files)
{
Console.WriteLine("File " + file);
// string NewFile = "";
FileInfo FI = new FileInfo(file);
// Do Stuff with FI
}
}
}
catch (Exception)
{
ErrorLog += "Folder to long " + folder + "\n";
continue;
}
This worked How might I capture the Exception reason and print this to the ErrorLog ?
Use type of Exception!
catch(System.IO.PathTooLongException exception)
{
ErrorLog += exception.Message;
}
This is the documentation about exception including example code:
https://msdn.microsoft.com/en-us/library/system.exception(v=vs.110).aspx
catch (Exception e)
{
//use e.Message or e.ToString() or .... here to log what went wrong
}
https://msdn.microsoft.com/en-us/library/system.exception(v=vs.110).aspx
catch (Exception e)
{
//use e.Message or e.ToString() or .... here to log what went wrong
}
ASKER
Hi
Thanx for the link is my understanding correct you use try / catch blocks where the code may encounter non program logic errors and nest 1 try within another something like bellow?
Where looping through directories is the first try block this has 2 possible exceptions, Access denied and everything else
and have a second try / catch block to handle problems with files
I assume you handle every possible exception in it's own catch unless like here you handle every problem with a file the same
Thanx for the link is my understanding correct you use try / catch blocks where the code may encounter non program logic errors and nest 1 try within another something like bellow?
Where looping through directories is the first try block this has 2 possible exceptions, Access denied and everything else
and have a second try / catch block to handle problems with files
I assume you handle every possible exception in it's own catch unless like here you handle every problem with a file the same
try{
string[] folders = Directory.GetDirectories(BaseFold, "*", SearchOption.AllDirectories);
// Do stuff with the folder
foreach (string folder in folders)
{
try{
string[] Files = Directory.GetFiles(folder);
int FilesCount = Files.Length;
// This is program logic therefore not an exception
if (FilesCount >=1)
{
foreach (string file in Files)
{
Console.WriteLine("File " + file);
FileInfo FI = new FileInfo(file);
// Do Stuff with FI
}
}
}
catch (Exception e)
{
// Log Error with a file
//use e.Message or e.ToString() or .... here to log what went wrong
}
}
}
catch (UnauthorizedAccessException ex)
{
// ok, so we are not allowed to dig into that directory. Move on.
continue;
}
catch (Exception e)
{
// Log all other Errors with directory
//use e.Message or e.ToString() or .... here to log what went wrong
continue;
}
Yes, try catch is for handling unexpected problems.
ASKER
@AndyAinscow with ref to last comments I was referring to the logic of using nested Try /catch 1 for handling directories 2 for files I'm guessing there is no right or wrong answer or best practice
Anyway thanx for your help
Anyway thanx for your help
Depends on the circumstances and just what you want. No general answer that covers all cases.
For that I'd just use one try catch - because it is all to do with processing those folders. However one could make a case for two try catch blocks.
For that I'd just use one try catch - because it is all to do with processing those folders. However one could make a case for two try catch blocks.
Try this:
Open in new window