Last active
April 8, 2021 16:40
-
-
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??
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; | |
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