Related Posts with Thumbnails

Exception Handling in .NET

Monday, May 3, 2010

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'.

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.

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.

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.

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 transferred to the catch block.

'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.


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.

0 comments:

Post a Comment

Site Rate