Last active
January 28, 2018 15:19
-
-
Save luisdeol/ebd7ebe4b8a745ff096b788e7a00cd23 to your computer and use it in GitHub Desktop.
Using the ExceptionDispatchInfo class
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.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