Skip to content

Commit 5b43caa

Browse files
committed
Improve exception handling.
1 parent 635863f commit 5b43caa

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

Program.cs

+48-9
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,29 @@ private static void Delete(string path, string displayPath)
4040
displayPath = "..." + displayPath.Substring (displayPath.Length-61, 61);
4141
}
4242

43-
if (System.IO.File.Exists (path))
43+
if (Program.ExecuteOperation (() => System.IO.File.Exists (path)))
4444
{
4545
System.Console.WriteLine ("Delete file {0}", displayPath);
46-
System.IO.File.Delete (path);
46+
47+
Program.DeleteFile (path);
48+
4749
return;
4850
}
4951

50-
if (System.IO.Directory.Exists (path))
52+
if (Program.ExecuteOperation (() => System.IO.Directory.Exists (path)))
5153
{
5254
var shortPath = Program.ShortenDir (path);
5355

5456
System.Console.WriteLine ("Analyzing {0}", displayPath);
5557

56-
var entries = System.IO.Directory.GetFileSystemEntries (shortPath);
58+
var entries = Program.ExecuteOperation (() => System.IO.Directory.GetFileSystemEntries (shortPath));
5759

58-
foreach (var entry in entries)
60+
if (entries != null)
5961
{
60-
Program.Delete (entry, System.IO.Path.Combine (fullDisplayPath, System.IO.Path.GetFileName (entry)));
62+
foreach (var entry in entries)
63+
{
64+
Program.Delete (entry, System.IO.Path.Combine (fullDisplayPath, System.IO.Path.GetFileName (entry)));
65+
}
6166
}
6267

6368
System.Console.WriteLine ("Delete dir {0}", displayPath);
@@ -66,18 +71,52 @@ private static void Delete(string path, string displayPath)
6671
}
6772
}
6873

69-
private static void DeleteDir(string newPath)
74+
private static void DeleteFile(string path)
75+
{
76+
Program.ExecuteOperation (() => System.IO.File.Delete (path));
77+
}
78+
79+
private static void DeleteDir(string path)
80+
{
81+
Program.ExecuteOperation (() => System.IO.Directory.Delete (path));
82+
}
83+
84+
private static T ExecuteOperation<T>(System.Func<T> operation)
85+
{
86+
T result = default (T);
87+
88+
Program.ExecuteOperation (() =>
89+
{
90+
result = operation ();
91+
});
92+
93+
return result;
94+
}
95+
96+
private static void ExecuteOperation(System.Action operation)
7097
{
7198
try
7299
{
73-
System.IO.Directory.Delete (newPath);
100+
operation ();
74101
}
75102
catch (System.IO.IOException ex)
76103
{
77-
System.Console.WriteLine (ex.Message);
104+
Program.Display (ex);
105+
}
106+
catch (System.UnauthorizedAccessException ex)
107+
{
108+
Program.Display (ex);
78109
}
79110
}
80111

112+
private static void Display(System.Exception ex)
113+
{
114+
var color = System.Console.ForegroundColor;
115+
116+
System.Console.ForegroundColor = System.ConsoleColor.Red;
117+
System.Console.Error.WriteLine (ex.Message);
118+
System.Console.ForegroundColor = color;
119+
}
81120

82121
private static string ShortenDir(string path)
83122
{

Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515

1616
[assembly: ComVisible (false)]
1717

18-
[assembly: AssemblyVersion ("1.0.1513.0")]
19-
[assembly: AssemblyFileVersion ("1.0.1513.0")]
18+
[assembly: AssemblyVersion ("1.1.1513.0")]
19+
[assembly: AssemblyFileVersion ("1.1.1513.0")]

0 commit comments

Comments
 (0)