C# INTERMEDIATEChapter 4 · C# Intermediate
Exception handling
C# uses try-catch-finally blocks to handle exceptions. The finally block executes whether an exception occurs or not, which is ideal for cleaning up resources.
Worked example
try {
int x = 0;
int y = 10 / x;
} catch (DivideByZeroException ex) {
Console.WriteLine("Math error: " + ex.Message);
} finally {
Console.WriteLine("Execution complete.");
}How it reads
- try blocks hold code that could throw an exception
- catch (DivideByZeroException) handles specific division by zero arithmetic errors
- finally always runs at the end of the error handling sequence

Cloud tip: Always list more specific catch blocks (e.g. DivideByZeroException) before a general catch-all Exception block.


