Locked lesson.
About this lesson
We discuss how to deal with Errors within your code and how Exception Handling can prevent your program from crashing.
Exercise files
Download this lesson’s related exercise files.
39 - Error and Exception Handling.docx57.6 KB 39 - Error and Exception Handling SOLUTION.docx
58.9 KB
Quick reference
Error and Exception Handling
Exception Handling allows us to catch errors without them crashing our software.
When to use
Use these whenever you have a block of code that may cause an error.
Instructions
try
{
string[] names = { "John", "Mary", "Tim" };
Console.WriteLine(names[5]);
}
catch (Exception e)
{
Console.WriteLine("Error " + e);
}
Hints & tips
- Try / Catch block
- Exception handling is great for catching errors
Lesson notes are only available for subscribers.