Created
October 9, 2023 09:30
-
-
Save ErgEnn/87e4d0528011bda26b6ee05da0aeffa8 to your computer and use it in GitHub Desktop.
[C# error handling]
This file contains 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
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 | |
} |
This file contains 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
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"); | |
} |
This file contains 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
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 | |
} | |
} |
This file contains 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
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