19. Exception Handling



You will learn about the following in this chapter:

  • What an exception is

  • How an uncaught exception abruptly terminates a program

  • How to use the try and catch blocks to catch and handle exceptions

  • How you can use the finally block to ensure a particular block of code is always executed

  • How multiple catch blocks allow you to catch and handle different exception types thrown from the same block of code

  • Nested try blocks

  • How to explicitly throw and rethrow an exception with the throw keyword

  • How to define your own exception types

The .NET execution engine generates an exception when a special condition arises during the runtime of a program. Special conditions are violations of the C# language rules and .NET execution environment. Dividing by zero, invalid casts, calling a non-existent file, and running out of memory are examples of special conditions. The ability to generate exceptions is not limited to the execution engine, you can also generate your own exceptions.

Exception handling allows you to deal with exceptions in a graceful manner by preventing the program from going down and let's you divide your code into two separate parts the code that provides the expected functionality of the program and the code that deals with the exceptional cases. This effectively prevents the, often, large amounts of exception handling code from intermingling with and thereby contaminating the normal case code, which then remains (relatively) easy to read.


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

Similar book on Amazon

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