Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class ArtificialStackTraceException : Exception
{
public ArtificialStackTraceException(
bool shouldIncludeFileInfo = true,
string message = "Artificial exception used to generate a stack trace."
) : base(message)
{
var stackTrace = new System.Diagnostics.StackTrace(shouldIncludeFileInfo);
StackTrace = stackTrace.ToString();
}
public override string StackTrace { get; }
public override string ToString()
{
return $"{nameof(ArtificialStackTraceException)}:{Environment.NewLine}{StackTrace}";
}
}
<Query Kind="Program" />
public class ArtificialStackTraceException : Exception
{
public ArtificialStackTraceException(
bool shouldIncludeFileInfo = true,
string message = "Artificial exception used to generate a stack trace."
) : base(message)
{
var stackTrace = new System.Diagnostics.StackTrace(shouldIncludeFileInfo);
StackTrace = stackTrace.ToString();
}
public override string StackTrace { get; }
public override string ToString()
{
return $"{nameof(ArtificialStackTraceException)}:{Environment.NewLine}{StackTrace}";
}
}
void Main()
{
new XElement("LINQPad.HTML", new XElement("h3", "new Exception")).Dump();
Foo(() => new Exception()).ToString().Dump();
new XElement("LINQPad.HTML", new XElement("h3", "Try...catch")).Dump();
Foo(() =>
{
try
{
throw new Exception();
}
catch (Exception ex)
{
return ex;
}
}).ToString().Dump();
new XElement("LINQPad.HTML", new XElement("h3", "new ArtificialStackTraceException")).Dump();
Foo(() => new ArtificialStackTraceException()).ToString().Dump();
}
Exception Foo(Func<Exception> generateException)
{
return Bar(generateException);
}
Exception Bar(Func<Exception> generateException)
{
return Baz(generateException);
}
Exception Baz(Func<Exception> generateException)
{
return generateException();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.