Skip to content

Instantly share code, notes, and snippets.

@felipeslongo
Created February 27, 2019 14:32
Show Gist options
  • Save felipeslongo/c1c6df5d95a9b0eceeec4c11ba757bb9 to your computer and use it in GitHub Desktop.
Save felipeslongo/c1c6df5d95a9b0eceeec4c11ba757bb9 to your computer and use it in GitHub Desktop.
How exception works
static void async Main(string[] args)
{
try
{
await ThrowExceptionAfterAsync();
}
catch(Exception e)
{
//Gonna catch SHU
Console.WrileLine(e.Message);//SHU
}
try
{
ThrowExceptionAfterAsync();//Exceptions thrown will be swallowed
}
catch(Exception e)
{
//Never gonna hit.
Console.WrileLine(e.Message);
}
try
{
ThrowExceptionBeforeAsync();
}
catch(Exception e)
{
//Gonna catch SHU
Console.WrileLine(e.Message);
}
}
public static async Task ThrowExceptionAfterAsync()
{
await Task.Delay(TimeSpan.FromSeconds(5));
throw new Exception("SHU");
}
public static async Task ThrowExceptionBeforeAsync()
{
throw new Exception("SHU");
await Task.Delay(TimeSpan.FromSeconds(5));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment