I am building my "base exception to end all base exceptions" class in C# and was looking for some opinions on my implementations so far. Ideally my usage of integrated Rethrow, Throw, and ThrowFatal methods for some syntactic sugar.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Schloss
{
[Serializable]
public class SchlossException : Exception
{
// member variables
private bool _fatal = false;
private bool _rethrown = false;
private ArrayList _additionalInfo = new ArrayList();
public bool Fatal { get { return _fatal; } }
public bool Rethrown { get { return _rethrown; } }
public ArrayList AdditionalInfo { get { return _additionalInfo; } }
public SchlossException() { }
public SchlossException(string message) : base(message) { }
public SchlossException(string message, Exception inner) : base(message, inner) { }
protected SchlossException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context) { }
// rethrow an exception to preserve the call stack across threads
public void Rethrow()
{
_rethrown = true;
this.Throw();
}
// throw this sexception
public void Throw()
{
throw this;
}
// throw with the fatal flag, in case anyone is listening o_O
public void ThrowFatal()
{
_fatal = true;
this.Throw();
}
// poopy, messy code
public SchlossException(string Msg, params object[] AddInfLst)
: base(Msg, SchlossException.GetInnerException(AddInfLst))
{
this.AppendAdditionalInfo(AddInfLst);
}
private static Exception GetInnerException(params object[] AddInfLst)
{
Exception Exc = null;
if (AddInfLst.Length > 1)
{
Exc = (AddInfLst[0] as Exception);
}
return Exc;
}
public void AppendAdditionalInfo(params object[] AddInfLst)
{
if (AddInfLst == null)
{
return;
}
for (int i = 0; i < AddInfLst.Length; i++)
{
object Obj = AddInfLst[i];
if (Obj is object[])
{
this.AppendAdditionalInfo((object[])Obj);
}
else
{
this._additionalInfo.Add(Obj);
}
}
}
private string GetAdditionalInfo(ArrayList AddInfLst)
{
string AddInfStr = "";
foreach (object Obj in AddInfLst)
{
string InfStr;
if (Obj is Exception)
{
InfStr = (Obj as Exception).ToString();
}
else
{
if (Obj is ArrayList)
{
InfStr = this.GetAdditionalInfo(AddInfLst);
}
else
{
InfStr = Obj.ToString();
}
}
if (AddInfStr.Length > 0 && InfStr.Length > 0)
{
AddInfStr += "\n";
}
AddInfStr += InfStr;
}
return AddInfStr;
}
public string GetMessageAndAdditionalInfo()
{
try
{
return this.GetBaseException().ToString() + " Additional Info: " + this.GetAdditionalInfo(this._additionalInfo);
}
catch
{
try
{
return this.GetBaseException().ToString();
}
catch
{
return "No Exception.";
}
}
}
}
}