Skip to content

Instantly share code, notes, and snippets.

@luisdeol
Last active January 28, 2018 15:19
Show Gist options
  • Save luisdeol/ebd7ebe4b8a745ff096b788e7a00cd23 to your computer and use it in GitHub Desktop.
Save luisdeol/ebd7ebe4b8a745ff096b788e7a00cd23 to your computer and use it in GitHub Desktop.
Using the ExceptionDispatchInfo class
using System;
using System.Runtime.ExceptionServices;
namespace implement_exception_handling
{
class Program
{
static void Main(string[] args)
{
var notSoGreatPhrase = "That is a not-so-great phrase";
ExceptionDispatchInfo exceptionDispatchInfo = null;
try
{
var phraseToInt = int.Parse(notSoGreatPhrase);
}
catch (FormatException exception)
{
exceptionDispatchInfo = ExceptionDispatchInfo.Capture(exception);
}
// "?" is a C# 6.0 feature. It allows you to call Functions on objects without the fear of a NulLReferenceException,
// since it wraps the null checking before executing the method.
exceptionDispatchInfo?.Throw();
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment