Section 16.11. Summary


16.11. Summary

  • Throwing (or raising) an exception halts execution of your program in place, and execution proceeds in the most immediately available catch block (exception handler). If the exception was not raised within a TRy block, or there is no catch block, the stack is unwound until a catch block is found. If no catch block is ever found, the built-in exception handler is invoked, which terminates your program with an error message.

  • An exception object can contain information about the circumstances that cause the exception to be raised. Typically, exception objects contain at least a text message explaining the exception.

  • Exceptions can be derived from and can contain other exceptions within them to allow for very flexible error handling.

  • A bug is a programming mistake that should be fixed before the program is released. An exception, however, is the result of a predictable but unpreventable problem that arises during run-time (for example, running out of disk space).

  • All exceptions used in C# derive from System.Exception , and all exceptions in your program should derive from System.Exception .

  • When a program encounters a problem that it cannot solve or work around, it may throw an exception to halt execution and allow the exception handler to fix the problem.

  • You throw an exception yourself using the throw keyword.

  • It is good programming practice to enclose code that has a high risk of throwing an exception within a TRy block and to provide an exception handler (a catch block) and perhaps a finally block.

  • The catch block follows the try block and contains the code used to handle the exception.

  • You can create dedicated catch statements to catch specific types of exceptions taking advantage of the inheritance hierarchy of exceptions.

  • Any action that must be taken whether or not an exception is raised (such as closing a file) should be enclosed in a finally block.



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net