Last active
June 10, 2019 03:29
-
-
Save danmoseley/03f6c6086f2d6937790bd8800cbee4f4 to your computer and use it in GitHub Desktop.
test exception to string
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; | |
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; | |
namespace _1 | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
//Console.ReadKey(); | |
var ex1 = OuterCaller("ArgumentException", "<outer1msg>"); | |
var ex2 = OuterCaller("async", "<outer2msg>"); | |
var ex3 = OuterCaller("FileNotFoundException", "<inner2msg>"); | |
SetInnerException(ex2, ex3); | |
var ex = new AggregateException("<aggmsg>", new[] { ex1, ex2 } ); | |
Console.WriteLine(ex); | |
Console.WriteLine("=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-="); | |
var ex4 = Wrap(null, () => throw new Interop.Crypto.OpenSslCryptographicException("error:25070067:DSO support routines:DSO_load:could not load the shared library")); | |
ex4 = Wrap(ex4, () => throw new TypeInitializationException("SslInitializer", null)); | |
ex4 = Wrap(ex4, () => throw new TypeInitializationException("Ssl", null)); | |
ex4 = Wrap(ex4, () => throw new TypeInitializationException("SslMethods", null)); | |
ex4 = Wrap(ex4, () => throw new AuthenticationException("Authentication failed, see inner exception.")); | |
ex4 = Wrap(ex4, () => throw new HttpRequestException("The SSL connection could not be established, see inner exception.")); | |
Console.WriteLine(ex4); | |
} | |
public static Exception Wrap(Exception inner, Action cb) | |
{ | |
try { | |
AAA(cb); | |
} | |
catch (Exception ex) { | |
SetInnerException(ex, inner); | |
return ex; | |
} | |
return null; | |
} | |
public static void AAA(Action cb) => BBB(cb); | |
public static void BBB(Action cb) => CCC(cb); | |
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(); | |
} | |
static void Caller(string type, string msg) => A(type, msg); | |
static void A(string type, string msg) | |
{ | |
ExceptionDispatchInfo.Throw(B(type, msg)); | |
} | |
static Exception B(string type, string msg) | |
{ | |
try | |
{ | |
C(type, msg); | |
} | |
catch(Exception ex) | |
{ | |
return ex; | |
} | |
return null; | |
} | |
static void C(string type, string msg) | |
{ | |
ExceptionDispatchInfo.Throw(D(type, msg)); | |
} | |
static Exception D(string type, string msg) | |
{ | |
try | |
{ | |
E(type, msg); | |
} | |
catch(Exception ex) | |
{ | |
return ex; | |
} | |
return null; | |
} | |
static void E(string type, string msg) => F(type, msg); | |
static void F(string type, string msg) | |
{ | |
if (type == "ArgumentException") | |
throw new ArgumentException(msg, "myarg"); | |
else if (type == "FileNotFoundException") | |
Something(msg); | |
} | |
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) | |
{ | |
if (inner == null) | |
return; | |
var f = typeof(Exception).GetField("_innerException", BindingFlags.NonPublic | BindingFlags.Instance ); | |
f.SetValue(outer, inner); | |
} | |
} | |
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