Chapter 16: Throwing and Catching Exceptions

Quiz Solutions

Solution to Question 16-1. An exception is an object (derived from System.Exception) that contains information about a problematic event. The framework supports throwing exceptions to stop processing and catching events to handle the problem and resume processing.

Solution to Question 16-2. The difference between a bug and an exception is that a bug is an error in programming, one that should be caught either by the compiler or in testing before you turn the program over to users. An exception is code that accounts for a situation that can’t be avoided during coding, but can be predicted, such as a lost database connection.

Solution to Question 16-3. To generate an exception, you use the throw keyword, although the system will generate some exceptions on its own.

Solution to Question 16-4. To handle an exception, you wrap the code you think might generate the exception in a try block. The code to handle the exception goes in an associated catch block.

Solution to Question 16-5. If no exception handler is found in the method that throws an event, the stack is unwound until a handler is found, or else the exception is handled by the CLR, which terminates the program.

Solution to Question 16-6. After the handler’s code is run, the program execution resumes with the code immediately following the handler (that is, after the catch block). Depending on where the handler is located in your code, and where the exception is thrown, you ...

Get Learning C# 3.0 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.