Skip to content

Instantly share code, notes, and snippets.

@mkropat
Last active May 23, 2017 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkropat/afc501468d669c97f027b52c3adc10b5 to your computer and use it in GitHub Desktop.
Save mkropat/afc501468d669c97f027b52c3adc10b5 to your computer and use it in GitHub Desktop.
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