[
Advertise | Submit Code | About us | Contact us | Link us
]
Go!
Membership Services
Login
Register

Home
C# General

General

C# Language

Design & Architecture

Algorithms

Database

Security

Active Directory

COM Interop

Remoting
C# Windows Forms

General

Combo and List boxes

Miscellaneous Controls

Button Controls

Edit Controls
Cutting Edge

ASP.NET 2.0

Visual Studio 2005

Windows Longhorn

SQL Server 2005
C# Multimedia and GDI+

General

DirectX

GDI+

Audio
Internet & Web

General

Images and multimedia

Database

Utilities

Security

ASP.NET Controls

Design and Architecture

Webservices
.NET

General

Design & Architecture

Algorithms

Database

Security

Active Directory

COM Interop

Remoting

ADO.NET

XML.NET

Tools

Enterprise

IDE
Visual Basic .NET

VB.NET General

VB.NET Controls
General Reading

.NET Books Review

Product Showcase

Book Chapters

Business Design & Strategy
Community

Discuss

Job Board

Discussion

CodeXchange
DeveloperLand

Advertise

Submit Code

About us

Contact us

Link us
Miscellaneous

Favorite Links

Downloads

Programming Sites

Top Stories
Regular Expressions

E-Mail

Date/Time
Home > C# General > General
Exception handling in .NET Framework
Posted by on Tuesday, November 30, 2004 (EST)

If you write a program, it is for sure that it will have errors and issues. You cannot avoid them. But what you can do is write the code such a way that it is easy to find the errors and issues so that you can solve them easily. You need the real skill to write 'maintainable code'.

This article has been viewed: 4,501 times
Technology: General.

Contents

Exception handling in .NET Framework

If you write a program, it is for sure that it will have errors and issues. You cannot avoid them. But what you can do is write the code such a way that it is easy to find the errors and issues so that you can solve them easily. You need the real skill to write 'maintainable code'.

Top Go to Table of Contents

What is maintainable code ?

If an existing application is given to a new employee in the company and ask him to fix a problem in that, most probably he will come back with the answer : "oh, it is stupid code.. I can't figure out what they have written. Instead of trouble shooting the issues in this code, I can re write the entire application with less time."

This is a common scenario in the programming world. The above answer shows the code is not a maintainable code. Over a period of time, there will be lot of changes required in existing applications to adapt to the new requirements. Only if you write 'maintainable code', you can enhance your application when new requirements come up and solve issues.

There is no specific set of rules to make an application 'maintainable'. There are lot of factors involved in it including following coding standards, writing lot of comments within code, writing self explanatory code, separating application into multiple well defined layers, having good exception handling etc.

Top Go to Table of Contents

What is an 'Exception' ?

'Exception' is an error situation that a program may encounter during runtime. For example, your program may be trying to write into a file, but your hard disk may be full. Or, the program may be trying to update a record in database, but that record may be already deleted. Such errors may happen any time and unless you handle such situation properly, your application may have un predictable behaviour.

Top Go to Table of Contents

What is the difference between 'Exception' and 'Error' ?

Error is an expected situation in an application. For example, you want to create a file in the folder "C:\Projects\Test\". If you attempt to create a file when this folder doesn't exists, it will make the operating system throw an exception. But you should not leave it to the operating system to throw the exception. This has to be handled through code. You must first check for the existence of the folder before you attempt to write the file. If the folder doesn't exists, you must first create the folder. This is how a stable application should be written.

Exception is a runtime error that the program may encounter during the execution of the program. For example, hard disk may be full when you attempt to write into a file, network connection may be lost while your application is communicating with another computer etc. There is no clear definition, but typically, Exceptions are unpredictable errors during runtime.

Top Go to Table of Contents

What is 'Exception Handling'?

When you ride a bike, you may wear a helmet. When you go for boating, you might use a life jacket. A car driver might use seat belts while driving at high speed in a high way. What is the purpose ? To handle exceptions (accidents), right ? We do not know when it might happen, so we are prepared to 'handle' such situations any time.

An exception can occur anytime during the execution of an application. Your application must be prepared to face such situations. An application will crash if an exception occurs and it is not handled.

"An exception handler is a piece of code which will be called when an exception occurs."

.NET Framework provides several classes to work with exceptions. The keywords try, catch are used to handle exceptions in .NET. You have to put the code (that can cause an exception) in a try block. If an exception occurs at any line of code inside a try block, the control of execution will be transfered to the code inside the catch block.

Syntax :

  try
  {
   // Code which can cause an exception.
  }
  catch(Exception ex)
  {
 // Code to handle exception
  }
  
Just like a bike rider wrap his head in a helmet to protect from accident, the above syntax will protect the code within the try block from accidents (exceptions).

If any statement within the try block raises an exception, the control of execution will be transfered to the first line within the catch block. You can write the error handling code in the catch block, like recording the error message into a log file, sending an email to the administrator about the problem occurred, showing an appropriate error message to the user etc.

See the following code :

  try
  {
   Statement 1
   Statement 2
   Statement 3
  }
  catch(Exception ex)
  {
   Statement 4
   Statement 5   
  }
            
  Statement 6
   

In normal flow of program, the statements 1, 2, 3, 6 will be executed. Statements 4 and 5 will be executed only if an exception occurs.

Assume there is an exception at statement 2. In this scenario, statements 1, 2, 4, 5 and 6 will be executed. Statement 3 will not be executed at all, because an exception occurred at statement 2 and program flow was transfered to the catch block.

Top Go to Table of Contents

'finally' block

You can optionally use a 'finally' block along with the try-catch. The 'finally' block is guaranteed to be executed even if there is an exception.

Sample Code :

  try
  {
   Statement 1
   Statement 2
   Statement 3
  }
  catch(Exception ex)
  {
   Statement 4
   Statement 5   
  }
  finally
  {
   Statement 6
   Statement 7
  }
            
  Statement 8
In normal flow of program, the statements 1, 2, 3, 6, 7 and 8 will be executed. Statements 4 and 5 will be executed only if an exception occurs.

Assume there is an exception at statement 2. In this scenario, statements 1, 2, 4, 5, 6, 7 and 8 will be executed.

Note that the statements 6 and 7 are executed in both the cases. The bottom line is, the code within the 'finally' block is executed whether there is an exception or not.

You can use the finally block to do any code cleanup. For example, you are doing some database operations inside a try block.

  try
  {
   Statement 1 - Open database
   Statement 2 - Execute Query
   Statement 3 - Close Database
  }
  catch(Exception ex)
  {
   Statement 4 - Show messagebox to user
  }
In the above code sample, what will happen if an exception occurs when the statement 2 is executed ? The catch block will be executed and a message box will be shown to the user. But the Statement 3 is never executed in that case, which means the database will not be closed.

Here is where the finally block will be helpful.
  try
  {
   Statement 1 - Open database
   Statement 2 - Execute Query
  }
  catch(Exception ex)
  {
   Statement 3 - Show messagebox to user
  }
  finally
  {
   Statement 4 - Close Database
  }
See the improved code. We have moved the code to close the database to the 'finally' block. The statement to close the database will be executed whether there is an exception or not.

Top Go to Table of Contents

Why should we catch exceptions ?

Why should you use a helmet when you ride a bike ? To protect your head from crashing when there is an accident, right ? Just like that, exception handling will protect your application from crashing when there is an exceptions.

If an exception is not 'handled' in code, the application will crash and user will see an ugly message. Instead, you can catch the exception, log the errors and show a friendly message to the user.

Visit
to see more C# tutorials [^].

Top Go to Table of Contents

About Manjaly John

Click here if you want to know more about .

Other articles that may interest you

  • Write a Word Add-In – Part 0
  • Write a Word Add-In – Part I
  • Lengthy Operations on Single Thread in .NET Application
  • Learning Draughts
  • Exceptions and Performance
  • Average Rating :

    Discussion Forums
    Got a programming related question? Hopefully someone has the answer... Want to help out other developers? Visit our discussion forums.

    Sponsored by:

    New Articles

  • Exceptions and Performance
    Almost every time exceptions are mentioned in mailing lists and newsgroups, people say they're really expensive.Let's examine that claim, shall we?

  • Creating multilingual websites - Part 1
    Extend the existing globalization capabilities of .NET to create flexible and powerful multilingual web sites. First, create a custom ResourceManager, and then create custom localized-capable server controls to easily deploy multilingual functionality.

  • Parameter passing in C#
    Many people have become fairly confused about how parameters are passed in C#, particularly with regard to reference types. This page should help to clear up some of that confusion

  • Most Popular Articles

  • LDAP, IIS and WinNT Directory Services
    This article explains how to use .NET Directory Services to retrieve and search directory objects, create new directory objects and edit or delete existing directory objects. Describes Active Directory Application Mode (ADAM) and how to use the IIS, WinNT and LDAP directory (ADSI) provider.

  • An in-depth look at WMI and instrumentation, Part II
    WMI stands for Windows Management Instrumentation and, as the name indicates, is about managing your IT infrastructure this article is the second part of a two-part series.

  • An in-depth look at WMI and instrumentation, Part I
    WMI stands for Windows Management Instrumentation and, as the name indicates, is about managing your IT infrastructure this article provides an in-depth look at WMI and MOM 2005

  • New Books

  • Murach's ASP.NET 2.0 Upgrader's Guide: VB Edition
    What’s new and how to use it! That’s what this book delivers if you’re a VB developer who’s interested in upgrading from ASP.NET 1.x to ASP.NET 2.0.

  • C# in easy steps
    Learn to program with Microsoft’s premier programming language. No previous programming knowledge is assumed. With numerous easy-to-follow examples, this title explains the essentials of object-oriented programming with C#.

  • Murach's ASP.NET web programming with VB.NET
    Murach's ASP.NET web programming with VB.NET by Doug Lowe and Anne Prince is a in depth training and reference book for ASP.NET programming using VB.NET. The book builds upon Murach's previous books and covers more advanced concepts for programming ASP.NET pages.

  • Got Code?

    if you have any article , source code , or anything else you'd like to share with this community that you think others might find useful, please submit it here and we will gladly make it available on this site. submit@developerland.com.
    Partners

    All articles are copyrighted by their individual authors unless otherwise specified , everything else Copyright ©2004-2006 DeveloperLand