Thursday, 12 January 2012

Exceptions in C#

General Discussion:
  Bug: A bug is a programmer mistake.
  Error: An error is caused by a user action.For eg: the user may enter a number where a letter is    expected.Errors can be catched with validation code.
  Exceptions:Even if you remove all bugs and anticipated errors, you may still run into problems, such as running out of memory or trying to open a file that is no longer exists.You cannot prevent Exceptions, but you can handle them so they do not bring down your program.
Idealy, if the exception is caught and handled, the program can fix the problem and continue. Even if the program cannot continue, by catching the exception you have the opportunity to print the error message and terminate the program in a meaningful way.
If there is code in your program that runs regardless of whether an Exception is occured(for eg: to release resources you have allocated), you can place that code in finally block,where it is certain to run,even in the presence of exceptions.
A try block must always be followed by a catch or finally block.
A finally block can be created with or without catch block, but a finally block requires a try block to execute.
It is an error to exit a finally block with break,continue,exit,gotoetc.
System.Exception
It provides a number of useful methods and properties.
The Message property provides information about the exception. It is read only
The HelpLink property provides a link to the help file associated with the exception. Read/write
The StackTrace property of the exception is used to provide a stack trace for the error statement. Read only

No comments:

Post a Comment