121
© Mikael Olsson 2020
M. Olsson,
C# 8 Quick Syntax Reference,
https://doi.org/10.1007/978-1-4842-5577-3_21
CHAPTER 21
Exception Handling
Exception handling allows programmers to deal with unexpected
situations that may occur in programs. As an example,
consider opening a
file using the StreamReader class in the System.IO namespace. To see what
kinds of exceptions this class may throw, you
can hover the cursor over
the class name in Visual Studio. For instance, you may see the System.IO
exceptions FileNotFoundException and DirectoryNotFoundException.
If any of those exceptions occurs, the program will
terminate with an error
message.
using System;
using System.IO;
class ErrorHandling
{
static void Main()
{
// Run-time error
StreamReader sr = new StreamReader("missing.txt");
}
}