Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@BenLubar
Created September 20, 2017 22:44
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 BenLubar/e67f3b3e56eddf0ff517b675ad0e81dc to your computer and use it in GitHub Desktop.
Save BenLubar/e67f3b3e56eddf0ff517b675ad0e81dc to your computer and use it in GitHub Desktop.
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
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);
}
}
}
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