try { // Some code that may throw an exception } catch (Exception ex) { // Handle the exception here Console.WriteLine($"Exception caught: {ex.Message}"); }
int numerator = 10; int denominator = 0; try { int result = numerator / denominator; } catch (DivideByZeroException ex) { Console.WriteLine($"Exception caught: {ex.Message}"); }In this example, we purposely try to divide by zero, which will throw a `DivideByZeroException`. We use a `try-catch` block to catch this exception and handle it accordingly. The `Exception` class is part of the `System` namespace in C#.