Skip to content

Instantly share code, notes, and snippets.

@danmoseley
Last active October 21, 2019 03:37
Show Gist options
  • Save danmoseley/e2263748f12a4edf81ef297dbd200c16 to your computer and use it in GitHub Desktop.
Save danmoseley/e2263748f12a4edf81ef297dbd200c16 to your computer and use it in GitHub Desktop.
test exceptions 2
using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Security;
using System.Security.Authentication;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
namespace _1
{
class Program
{
public static void Main(string[] args)
{
//Console.ReadKey();
Console.WriteLine(Wrap(() => throw new InvalidOperationException("<some regular exception>")));
Console.WriteLine();
Console.WriteLine();
var ex1 = OuterCaller("ArgumentException", "<outer1msg>");
var ex2 = OuterCaller("async", "<outer2msg>");
var ex3 = OuterCaller("FileNotFoundException", "<inner2msg>");
SetInnerException(ex2, ex3);
var exA = new AggregateException("<aggmsg>", new[] { ex1, ex2 });
Console.WriteLine(exA);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
var exB = Wrap(() => throw new Interop.Crypto.OpenSslCryptographicException("error:25070067:DSO support routines:DSO_load:could not load the shared library"));
exB = Wrap(exB, () => throw new TypeInitializationException("SslInitializer", null));
exB = Wrap(exB, () => throw new TypeInitializationException("Ssl", null));
exB = Wrap(exB, () => throw new TypeInitializationException("SslMethods", null));
exB = Wrap(exB, () => throw new AuthenticationException("Authentication failed, see inner exception."));
exB = Wrap(exB, () => throw new HttpRequestException("The SSL connection could not be established, see inner exception."));
Console.WriteLine(exB);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
var ee = new ExternalException("<eemsg>");
var ce = new COMException("<cemsg>");
var bie = Wrap(() => CCC(() => Assembly.LoadFrom(@"c:\\windows\\system32\\kernelbase.dll")));
//var fle = Wrap(() => CCC(() => { var fn = new AssemblyName() { Name = "" }.FullName; }));
var fle = Wrap(() => CCC(() => throw new FileLoadException("<flemsg>", @"c:\some\file")));
var fnf = Wrap(() => CCC(() => Assembly.LoadFrom(@"c:\non\existent\file.dll")));
var fnf2 = Wrap(() => Assembly.LoadFrom(@"C:\Windows\Microsoft.NET\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll")); // 32 bit
SetInnerException(ee, ce, bie, fle, fnf);
SetInnerException(fnf, fnf2);
Console.WriteLine(ee);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static Exception Wrap(Action cb)
{
try
{
cb();
}
catch (Exception ex)
{
return ex;
}
Debug.Fail("?");
return null;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static Exception Wrap(Exception inner, Action cb)
{
try
{
AAA(cb);
}
catch (Exception ex)
{
SetInnerException(ex, inner, null);
return ex;
}
return null;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void AAA(Action cb) => BBB(cb);
[MethodImpl(MethodImplOptions.NoInlining)]
public static void BBB(Action cb) => CCC(cb);
[MethodImpl(MethodImplOptions.NoInlining)]
public static void CCC(Action cb) => cb();
public static Exception OuterCaller(string type, string msg)
{
try
{
if (type == "async")
{
MethodAsync(msg, 1, 2, 3, 4).GetAwaiter().GetResult();
}
else
{
Caller(type, msg);
}
}
catch (Exception ex)
{
return ex;
}
return new Exception();
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void Caller(string type, string msg) => A(type, msg);
[MethodImpl(MethodImplOptions.NoInlining)]
static void A(string type, string msg)
{
ExceptionDispatchInfo.Throw(B(type, msg));
}
[MethodImpl(MethodImplOptions.NoInlining)]
static Exception B(string type, string msg)
{
try
{
C(type, msg);
}
catch (Exception ex)
{
return ex;
}
return null;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void C(string type, string msg)
{
ExceptionDispatchInfo.Throw(D(type, msg));
}
[MethodImpl(MethodImplOptions.NoInlining)]
static Exception D(string type, string msg)
{
try
{
E(type, msg);
}
catch (Exception ex)
{
return ex;
}
return null;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void E(string type, string msg) => F(type, msg);
[MethodImpl(MethodImplOptions.NoInlining)]
static void F(string type, string msg)
{
if (type == "ArgumentException")
throw new ArgumentException(msg, "myarg");
else if (type == "FileNotFoundException")
Something(msg);
else
Debug.Fail("?");
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void Something(string msg) => throw new FileNotFoundException(msg, "foo.txt");
static async Task<int> MethodAsync(string msg, int v0, int v1, int v2, int v3) => await MethodAsync(msg, v0, v1, v2);
static async Task<int> MethodAsync(string msg, int v0, int v1, int v2) => await MethodAsync(msg, v0, v1);
static async Task<int> MethodAsync(string msg, int v0, int v1) => await MethodAsync(msg, v0);
static async Task<int> MethodAsync(string msg, int v0) => await MethodAsync(msg);
static async Task<int> MethodAsync(string msg) => throw new InvalidCastException(msg, 123);
private static void SetInnerException(Exception outer, Exception inner, Exception inner2 = null, Exception inner3 = null, Exception inner4 = null, Exception inner5 = null)
{
if (inner == null)
return;
var f = typeof(Exception).GetField("_innerException", BindingFlags.NonPublic | BindingFlags.Instance);
f.SetValue(outer, inner);
if (inner2 != null)
f.SetValue(inner, inner2);
if (inner3 != null)
f.SetValue(inner2, inner3);
if (inner4 != null)
f.SetValue(inner3, inner4);
if (inner5 != null)
{
f.SetValue(inner4, inner5);
f.SetValue(inner5, new Exception("<reallyinner>"));
}
}
}
public class Interop
{
public class Crypto
{
public class OpenSslCryptographicException : Exception
{
public OpenSslCryptographicException(string msg) : base(msg) { }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment