Skip to content

Instantly share code, notes, and snippets.

@itn3000
Created July 7, 2021 07:53
Show Gist options
  • Select an option

  • Save itn3000/73df3fe206c239e380397c9ad0143e48 to your computer and use it in GitHub Desktop.

Select an option

Save itn3000/73df3fe206c239e380397c9ad0143e48 to your computer and use it in GitHub Desktop.
Inheriting only exception's StackTrace property.
using System;
namespace exceptionlabs
{
class InheritException : Exception
{
string _StackTrace;
public InheritException(Exception exception) : base(exception.Message)
{
_StackTrace = exception.StackTrace + base.StackTrace;
}
public override string StackTrace => _StackTrace;
}
class Program
{
static void A()
{
throw new Exception("hogehoge");
}
static void B()
{
A();
}
static void C()
{
try
{
B();
}
catch (Exception e)
{
throw new InheritException(e);
}
}
static void Main(string[] args)
{
try
{
C();
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
}
}
exceptionlabs.InheritException: hogehoge
at exceptionlabs.Program.A() in D:\src\gitrepos\dotnet-sandbox\exceptionlabs\Program.cs:line 18
at exceptionlabs.Program.B() in D:\src\gitrepos\dotnet-sandbox\exceptionlabs\Program.cs:line 22
at exceptionlabs.Program.C() in D:\src\gitrepos\dotnet-sandbox\exceptionlabs\Program.cs:line 28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment