- Joined
- Oct 24, 2012
- Messages
- 6,545
Hello everyone. I was making an Exception handling system that catches and handles different exceptions through calling functions that are stored in a dictionary based on the type.
I wanted to know if there are any downsides to the way I have done things and if there are any ways to improve it. ( not all exceptions are being handled yet, it is only a mock-up).
If there are any optimizations I can make it is appreciated.
Thanks for any help.
I wanted to know if there are any downsides to the way I have done things and if there are any ways to improve it. ( not all exceptions are being handled yet, it is only a mock-up).
If there are any optimizations I can make it is appreciated.
Thanks for any help.
JASS:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ExceptionMonitoring
{
class Exceptions
{
private string StrExceptionsFilePath { get; set; }
public bool BoolRegisterNewExceptions { get; set; }
private Dictionary<Type, ExceptionDelegate> DictTypeDelegate = new Dictionary<Type, ExceptionDelegate>();
private Dictionary<Type, string> DictFilePaths = new Dictionary<Type, string>();
private delegate string[] ExceptionDelegate(Exception ex);
public Exceptions()
{
BoolRegisterNewExceptions = true;
FileInfo path = new FileInfo("Exceptions.txt");
StrExceptionsFilePath = path.FullName;
DictTypeDelegate.Add(typeof(FileNotFoundException), StandardExc);
//DictFilePaths.Add(typeof(FileNotFoundException), StrExceptionsFilePath); // Not needed unless file paths are different.
}
private string[] StandardExc(Exception ex)
{
return new string[] { "Date and Time of Error is " + DateTime.Now.ToString(),
"The type of Exception is " + ex.GetType().ToString(), ex.Message,
"Stack trace = " + ex.StackTrace, " " };
}
public void SaveException(Exception ex)
{
try
{
Type t = ex.GetType();
if (DictTypeDelegate.ContainsKey(t))
{
SaveError((DictTypeDelegate[t](ex)), (DictFilePaths.ContainsKey(t)) ? DictFilePaths[t] : StrExceptionsFilePath);
}
else if (BoolRegisterNewExceptions)
{
DictTypeDelegate.Add(t, StandardExc);
SaveException(ex);
}
}
catch (Exception exc)
{
}
}
public static void SaveError(string[] str, string filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
foreach (string s in str)
{
sw.WriteLine(s);
}
}
}
}
}
JASS:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ExceptionMonitoring
{
class Exceptions
{
private string StrExceptionsFilePath { get; set; }
private Dictionary<Type, ExceptionDelegate> DictTypeDelegate = new Dictionary<Type, ExceptionDelegate>();
private Dictionary<Type, string> DictFilePaths = new Dictionary<Type, string>();
public Exceptions()
{
FileInfo path = new FileInfo("Exceptions.txt");
StrExceptionsFilePath = path.FullName;
DictTypeDelegate.Add(typeof(FileNotFoundException), StandardExc);
DictTypeDelegate.Add(typeof(DirectoryNotFoundException), StandardExc);
//DictTypesFuncts.Add(typeof(System.Reflection.TargetParameterCountException), new Func<Exception, string[]>(StandardExc));
//DictFilePaths.Add(typeof(FileNotFoundException), StrExceptionsFilePath); // Not needed unless file paths are different.
}
public delegate string[] ExceptionDelegate(Exception ex);
public string[] StandardExc(Exception ex)
{
return new string[] { "Date and Time of Error is " + DateTime.Now.ToString(),
"The type of Exception is " + ex.GetType().ToString(), ex.Message,
"Stack trace = " + ex.StackTrace, " " };
}
public void SaveException(Exception ex)
{
try
{
Type t = ex.GetType();
if (DictTypeDelegate.ContainsKey(t))
{
string path;
if (DictFilePaths.ContainsKey(t))
{
path = DictFilePaths[t];
}
else
{
path = StrExceptionsFilePath;
}
SaveError(((string[])DictTypeDelegate[t](ex)), path);
}
else
{
MessageBox.Show("The type " + t.ToString() + " has not been added yet.");
}
}
catch (Exception exc)
{
MessageBox.Show("Error Recieved = " + exc.GetType().ToString() + " " + exc.Message);
}
}
public static void SaveError(string[] str, string filePath)
{
File.WriteAllLines(filePath, File.ReadAllLines(filePath).Concat(str));
}
}
}
JASS:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ExceptionMonitoring
{
class Exceptions
{
private string StrExceptionsFilePath { get; set; }
private Dictionary<Type, Delegate> DictTypesFuncts = new Dictionary<Type, Delegate>();
private Dictionary<Type, string> DictFilePaths = new Dictionary<Type, string>();
public Exceptions()
{
FileInfo path = new FileInfo("Exceptions.txt");
StrExceptionsFilePath = path.FullName;
DictTypesFuncts.Add(typeof(FileNotFoundException), new Func<Exception, string[]>(StandardExc));
//DictFilePaths.Add(typeof(FileNotFoundException), StrExceptionsFilePath); // Not needed unless file paths are different.
}
public string[] StandardExc(Exception ex)
{
return new[] { "Date and Time of Error is " + DateTime.Now.ToString(),
"The type of Exception is " + ex.GetType().ToString(), ex.Message,
"The error is in " + ex.TargetSite.ToString(), " " };
}
public void SaveException(Exception ex)
{
Type t = ex.GetType();
if (DictTypesFuncts.ContainsKey(t))
{
string path;
if (DictFilePaths.ContainsKey(t))
{
path = DictFilePaths[t];
}
else
{
path = StrExceptionsFilePath;
}
SaveError((string[])DictTypesFuncts[t].DynamicInvoke(ex), path);
}
else
{
MessageBox.Show("The type " + t.ToString() + " has not been added yet.");
}
}
public static void SaveError(string[] str, string filePath)
{
File.WriteAllLines(filePath, File.ReadAllLines(filePath).Concat(str));
}
}
}
Last edited: