15.2. C# Exception Handling
Exceptions are a way for the .NET runtime to signal that something has gone wrong during program execution—that something could potentially cause a program to abort. One such situation would be trying to access a nonexistent object, as in the following example:
Student s1 = new Student(); Student s2 = null; s1.Name = "Fred"; // this line is fine s2.Name = "Mary"; // this line throws an exception
Let's explore what is happening in the preceding code.
We declare two Student object references but only instantiate one Student object; s2 is assigned the value null, indicating that it isn't presently holding onto any object.
Student s1 = new Student(); Student s2 = null;
When we subsequently attempt to assign a value to ...
Get Beginning C# 2008 Objects: From Concept to Code now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.