Some operations fail for reasons outside your control: text that is not a number, a file that is not there. Left alone, the program stops dead. try / catch lets you respond.
Program.cs
using System;
class Program
{
static void Main()
{
string input = "abc";
try
{
int number = int.Parse(input);
Console.WriteLine(number * 2);
}
catch (FormatException)
{
Console.WriteLine("That is not a number");
}
}
}
Output
That is not a number
The try block is attempted. The moment something throws, the rest of the block is abandoned and the matching catch runs. The program stays alive.
Do not swallow everything
A bare catch { } that does nothing hides real bugs and makes them unfindable later. Catch what you can actually handle.