Skip to content

Instantly share code, notes, and snippets.

@Ruffo324
Last active April 8, 2021 16:40
Show Gist options
  • Save Ruffo324/d5447ae8a899a90779e08ef8dae8d41a to your computer and use it in GitHub Desktop.
Save Ruffo324/d5447ae8a899a90779e08ef8dae8d41a to your computer and use it in GitHub Desktop.
Handle TryCatch inside a function example, for nested throwing?? If there would be any reason to do this??
using System;
namespace ConsoleApplication1
{
internal class Program
{
public static void Main(string[] args)
{
NestedCatching<SnowBallException>(ThrowSnowball,
() => NestedCatching<SnowBallException2>(ThrowSnowball2));
}
public static void ThrowSnowball() => throw new SnowBallException();
public static void ThrowSnowball2() => throw new SnowBallException();
public static void NestedCatching<TCatchException>(Action unsafeAction, Action? onFail = null)
where TCatchException : Exception
{
try
{
unsafeAction();
}
catch (TCatchException)
{
if (onFail is null)
throw; // No OnFailAction given? Let's poke the debugger xD
onFail();
}
}
internal class SnowBallException2 : SnowBallException
{}
internal class SnowBallException : Exception
{}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment