This chapter introduced exceptions, what they are, and how to handle them. The important points discussed in this chapter are as follows.

An exception represents an abnormal condition in your program.

An exception is an object of the class System.Exception or of a subclass derived thereof. The .NET Framework contains several intrinsic exception classes derived from System.Exception.

An exception object can either be implicitly created and thrown by the runtime, or explicitly created and thrown by applying the throw keyword in your source code.

An uncaught exception causes a program to stop abruptly and display (to many) an obscure message on the console.

Three different but closely related language elements are used to deal with exceptions:

  • try blocks are positioned inside functions enclosing code that may throw exceptions but which, otherwise, perform the normal operations of your program.

  • catch blocks contain the code that performs the corrective actions necessary to resolve the problems caused by an exception.

  • finally blocks guarantee that the code they contain is executed before execution exits from its associated try-catch construction.

Each catch block matches a particular type of exception. A try block can have several associated catch blocks, each dealing with a different exception type thrown from within the try block.

try blocks can be nested inside each other.

The throw keyword can be used from within a catch block to either rethrow an exception or throw a new exception. This allows several catch statements to handle the same original exception.

Custom exception classes can be written by deriving them from the System.ApplicationException class.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: