Back to course

Exception Handling (Try...Catch)

C# Zero to Hero: Comprehensive Programming Masterclass

Handling Errors

Errors during program execution are called Exceptions. If not handled, the program crashes.

Try...Catch

csharp try { int[] numbers = {1, 2, 3}; Console.WriteLine(numbers[10]); } catch (Exception e) { Console.WriteLine("Something went wrong: " + e.Message); } finally { Console.WriteLine("Execution finished."); }

finally always runs, regardless of an error.