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.