Back In the print window choose "Save as PDF" as the destination
NOTE.CS

Exception Handling in C#

1 topic ยท 28 Nov 2026
1

Exception Handling in C#

C#
C#
try
{
    int x = int.Parse("abc");
}
catch (FormatException ex)
{
    Console.WriteLine("Invalid number: " + ex.Message);
}
catch (Exception ex)
{
    Console.WriteLine("Other error: " + ex.Message);
}
finally
{
    Console.WriteLine("Always runs");   // cleanup
}

// using disposes the object automatically (calls Dispose)
using var reader = new StreamReader("data.txt");

Catch specific exceptions first. Use throw; (not throw ex;) to re-throw and keep the stack trace.