Created
July 7, 2021 07:53
-
-
Save itn3000/73df3fe206c239e380397c9ad0143e48 to your computer and use it in GitHub Desktop.
Inheriting only exception's StackTrace property.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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