Friday, July 6, 2012

Exception Handling

An exception is an error that occurs at runtime. Using C#’s exception handling subsystem, you can, in a structured and controlled manner, handle runtime errors. C#’s approach to exception handling is a blend of and improvement on the exception handling mechanisms used by C++ and Java. Thus, it will be familiar territory to readers with a background in either of these languages. What makes C#’s exception handling unique, however, is its clean, straightforward implementation.

A principal advantage of exception handling is that it automates much of the error-handling code that previously had to be entered “by hand” into any large program. For example, in a computer language without exception handling, error codes must be returned when a method fails, and these values must be checked manually, each time the method is called. This approach is both tedious and error-prone. Exception handling streamlines error-handling by allowing your program to define a block of code, called an exception handler, that is executed automatically when an error occurs. It is not necessary to manually check the success or failure of each specific operation or method call. If an error occurs, it will be processed by the exception handler.

Another reason that exception handling is important is that C# defines standard exceptions for common program errors, such as divide-by-zero or index-out-of-range. To respond to these errors, your program must watch for and handle these exceptions.
In the final analysis, to be a successful C# programmer means that you are fully capable of navigating C#’s exception handling subsystem.

Refer from C# books....

No comments:

Post a Comment