Created
September 20, 2017 22:44
-
-
Save BenLubar/e67f3b3e56eddf0ff517b675ad0e81dc to your computer and use it in GitHub Desktop.
This file contains 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
System.ArgumentNullException: Value cannot be null. | |
Parameter name: obj | |
at ExceptionStackKeeper.Program1.BarAsync[T](T obj) in C:\Users\Owner\Source\Repos\ExceptionStackKeeper\ExceptionStackKeeper\Program1.cs:line 41 | |
at ExceptionStackKeeper.Program1.<FooAsync>d__1.MoveNext() in C:\Users\Owner\Source\Repos\ExceptionStackKeeper\ExceptionStackKeeper\Program1.cs:line 35 | |
System.ArgumentNullException: Value cannot be null. | |
Parameter name: obj | |
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() | |
at ExceptionStackKeeper.Program2.Main(String[] args) in C:\Users\Owner\Source\Repos\ExceptionStackKeeper\ExceptionStackKeeper\Program2.cs:line 19 | |
System.ArgumentNullException: Value cannot be null. | |
Parameter name: obj | |
at ExceptionStackKeeper.Program2.Main(String[] args) in C:\Users\Owner\Source\Repos\ExceptionStackKeeper\ExceptionStackKeeper\Program2.cs:line 27 |
This file contains 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; | |
using System.IO; | |
using System.Runtime.ExceptionServices; | |
using System.Runtime.Serialization.Formatters.Binary; | |
using System.Threading.Tasks; | |
namespace ExceptionStackKeeper | |
{ | |
class Program1 | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
Task.Run(() => FooAsync()).Wait(); | |
} | |
catch (AggregateException ex) | |
{ | |
var formatter = new BinaryFormatter(); | |
using (var stream = new MemoryStream()) | |
{ | |
formatter.Serialize(stream, ex); | |
Console.Write("new byte[] {"); | |
foreach (var b in stream.ToArray()) | |
{ | |
Console.Write(" " + b + ","); | |
} | |
Console.WriteLine(" };"); | |
} | |
} | |
Console.ReadLine(); | |
} | |
private static async Task FooAsync() | |
{ | |
await BarAsync<string>(null); | |
} | |
private static Task<T> BarAsync<T>(T obj) | |
{ | |
if (object.Equals(obj, default(T))) | |
throw new ArgumentNullException(nameof(obj)); | |
return Task.FromResult(obj); | |
} | |
} | |
} |
This file contains 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; | |
using System.IO; | |
using System.Runtime.ExceptionServices; | |
using System.Runtime.Serialization.Formatters.Binary; | |
using System.Threading.Tasks; | |
namespace ExceptionStackKeeper | |
{ | |
class Program2 | |
{ | |
static void Main(string[] args) | |
{ | |
using (var data = new MemoryStream(/* put Program1 output here */)) | |
{ | |
var formatter = new BinaryFormatter(); | |
var ex = (AggregateException)formatter.Deserialize(data); | |
Console.WriteLine(ex.InnerException.ToString()); | |
try | |
{ | |
ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); | |
} | |
catch (ArgumentNullException ex1) | |
{ | |
Console.WriteLine(ex1.ToString()); | |
} | |
try | |
{ | |
throw ex.InnerException; | |
} | |
catch (ArgumentNullException ex1) | |
{ | |
Console.WriteLine(ex1.ToString()); | |
} | |
} | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment