Back to course

Exception Handling: Try, Catch, Finally

.NET Zero to Hero: Master C# and Modern App Development

Handling Errors

Prevent your app from crashing using try-catch blocks.

Syntax:

csharp try { int[] myNumbers = {1, 2, 3}; Console.WriteLine(myNumbers[10]); // Error! } catch (Exception e) { Console.WriteLine("Something went wrong: " + e.Message); } finally { Console.WriteLine("The 'try catch' is finished."); }

finally always executes, regardless of an error.