Skip to content

Instantly share code, notes, and snippets.

@ErgEnn
Created October 9, 2023 09:30
Show Gist options
  • Save ErgEnn/87e4d0528011bda26b6ee05da0aeffa8 to your computer and use it in GitHub Desktop.
Save ErgEnn/87e4d0528011bda26b6ee05da0aeffa8 to your computer and use it in GitHub Desktop.
[C# error handling]
internal class Disp : IDisposable
{
public void Dispose()
{
Console.WriteLine("dispose"); //<----- called
}
}
static void Main(string[] args)
{
try
{
using (new Disp())
{
Console.WriteLine("a"); //<----- called
if (Random.Shared.Next() > -1)
{
Console.WriteLine("b"); //<----- called
throw new Exception("b"); //<----- called
}
}
Console.WriteLine("c");
}catch
{
Console.WriteLine("d"); //<----- called
}
finally
{
Console.WriteLine("e"); //<----- called
}
Console.WriteLine("f"); //<----- called
}
internal class Disp : IDisposable
{
public void Dispose()
{
Console.WriteLine("dispose");
}
}
static void Main(string[] args) //<----- exits with unhandled error
{
try
{
using (new Disp())
{
Console.WriteLine("a"); //<----- called
if (Random.Shared.Next() > -1)
{
Console.WriteLine("b"); //<----- called
throw new Exception("b"); //<----- called
}
}
Console.WriteLine("c");
}
finally
{
Console.WriteLine("e");
}
Console.WriteLine("f");
}
static void Main(string[] args)
{
try
{
Console.WriteLine("a"); //<----- called
try
{
Console.WriteLine("b"); //<----- called
if (Random.Shared.Next() > -1)
{
Console.WriteLine("c"); //<----- called
throw new Exception("c");
}
Console.WriteLine("d");
}
finally
{
Console.WriteLine("e"); //<----- called
}
Console.WriteLine("f");
}
catch
{
Console.WriteLine("g"); //<----- called
}
}
static void Main(string[] args) //<----- exits with unhandled error
{
Console.WriteLine("a"); //<----- called
try
{
Console.WriteLine("b"); //<----- called
if (Random.Shared.Next() > -1)
{
Console.WriteLine("c"); //<----- called
throw new Exception("c");
}
Console.WriteLine("d");
}
finally
{
Console.WriteLine("e");
}
Console.WriteLine("f");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment