Hallo,
ich hab éin kleines Problem an dem ich grad nicht weiterkomme.
Ich will alle Exceptions + InnereExceptions durchlaufen und jeweils bestimmte Werte davon auslesen und speichern (wird nachher in einer XML-Datei gespeichert)
Die Struktur Ex->InnerEx->InnerEx->.. soll dabei erhalten bleiben.
Ich hab folgende Klasse in der die Daten speichere:
public class ExceptionInfos
{
public ExceptionInfos() { }
public ExceptionInfos(string exMsg, string exType)
{
this.ExceptionType = exType;
this.ExceptionMessage = exMsg;
}
public string ExceptionMessage { get; set; }
public string ExceptionType { get; set;}
public string StackTrace { get; set; }
public string AdditionalInformation { get; set; }
public ExceptionInfos InnerException;
public List<ExceptionInfos> exCol;
}
Mein Versuch das ganze zu füllen sieht so aus:
public void WriteToXML(Exception ex)
{
ExceptionInfos exInfos = new ExceptionInfos();
exInfos.ExceptionMessage = ex.Message;
exInfos.ExceptionType = ex.GetType().ToString();
exInfos.StackTrace = ex.StackTrace;
ExceptionInfos exInfos2 = new ExceptionInfos();
Exception ex2 = ex.InnerException;
while (ex2 != null)
{
exInfos2 = new ExceptionInfos();
exInfos2.ExceptionMessage = ex2.Message;
exInfos2.ExceptionType = ex2.GetType().ToString();
exInfos2.StackTrace = ex2.StackTrace;
Console.WriteLine(exInfos2.ExceptionMessage);
ex2 = ex2.InnerException;
}
Das Problem ist hierbei das mir die InnereException immer wieder ersetzt wird und ich keine komplette Verschachtelung habe wie ich es gern hätte.
Ich komme im Moment nicht auf die Idee wie ich das lösen könnte, das ich die Verschachtelung behalte. Ich hab schon rumprobiert, aber ich komm nicht auf die Lösung.
Wäre super, wenn mir da jemand auf die Sprünge hilft...
Rekursion ist hier die Lösung:
public void FillStore(Exception ex, ExceptionInfos exStore)
{
exStore.ExceptionMessage = ex.Message;
exStore.ExceptionType = ex.GetType().ToString();
exStore.StackTrace = ex.StackTrace;
exStore.InnerException = new ExceptionInfos();
FillStore(ex.InnerException, exStore.InnerException);
}
//Aufruf:
catch(Exception ex)
{
ExceptionInfos store = new ExceptionInfos();
FillStore(ex, store);
...
}
Besten dank, ich wusste doch das es dafür ne einfaceh Lösung gibt, bin da aber nicht drauf gekommen...
Ein Hinweis noch:;
du musst noch eine Prüfung auf null durchführen, sonst kriegst du eine Exception (Objekt nicht auf Objektinstanz festgelegt)
if(ex != null)
{
exStore.ExceptionMessage = ex.Message;
exStore.ExceptionType = ex.GetType().ToString();
exStore.StackTrace = ex.StackTrace;
exStore.InnerException = new ExceptionInfos();
FillStore(ex.InnerException, exStore.InnerException);
}
}
Wenn dann aber gleich so:
public void FillStore(Exception ex, ExceptionInfos exStore)
{
exStore.ExceptionMessage = ex.Message;
exStore.ExceptionType = ex.GetType().ToString();
exStore.StackTrace = ex.StackTrace;
exStore.InnerException = new ExceptionInfos();
if (ex.InnerException != null)
{
this.FillStore(ex.InnerException, exStore.InnerException);
}
}
Again what learned...
Hallo zusammen,
da die Verkettung der InnerExceptions einer einfachen Liste entspricht, ist Rekursion hier nicht von Nöten. Eine einfache Schleife würde es auch tun. In der ursprünglichen Schleife von Siedlerchr fehlt nur das Setzen von ExceptionInfos.InnerException.
herbivore