Try-Catch-Finally in C#

From the C# Programming Language cheat sheet · Exception Handling · verified Jul 2026

Try-Catch-Finally

Basic exception handling patterns

csharp
// Basic try-catch
try
{
    int result = 10 / 0;
}
catch (DivideByZeroException ex)
{
    Console.WriteLine($"Error: {ex.Message}");
}
finally
{
    Console.WriteLine("Cleanup");
}
💡 Catch specific exceptions before general ones
⚡ using statement ensures proper resource disposal
📌 finally block always executes, even after return
🟢 Exception filters allow conditional catch blocks
Back to the full C# Programming Language cheat sheet