Skip to content

Instantly share code, notes, and snippets.

@lgolubyev
Created July 11, 2022 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgolubyev/c24a01b5e20ac1771db828cb1a1bf971 to your computer and use it in GitHub Desktop.
Save lgolubyev/c24a01b5e20ac1771db828cb1a1bf971 to your computer and use it in GitHub Desktop.
var exceptionTrappingLazy = new Lazy(() => CallExternalService());
try{
// Accessing the Value property will execute CallExternalSe4rvice()
// Now, imagine that the external service is down
// The call to CallExternalService() throws an exception
var lazyValue = exceptionTrappingLazy.Value;
}
catch {...}
try
{
//Accesing the Value property again won't call CallExternalService()
//It will throw the trapped exception again!
var lazyValueAgain = exceptionTrappingLazy.Value;
}
catch{...}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment