C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Working with Exceptions - Part I (Unstructed Exception Handling).


Posted Date: 10 Mar 2004    Resource Type: Articles    Category: .NET Framework
Author: Sadha SivamMember Level: Gold    
Rating: Points: 7



In this section of article I will be telling about Exceptions, types of exceptions and I will tell in details about the Unstructured exception handling.

Introduction
An exception is termed as an abnormal condition encountered by an application during execution.

Exception Handling is the process of providing an alternate path to be executed when the application is unable to execute in the desired way.

Is this Exception Handling Necessary ?


  • By Handling exceptions, we can the prevent the termination of our application.
    For example, Most of the project involves working with databases. Suppose, let us assume that we are working in a web application, where we have one login page, which takes the user name and password, checks with the database for authorizing the user.

    Now if the Database Server is down, we will be getting an error (Exception) stating that the db server is down, that will be displayed to the user if not handled.

    Using Exception Handling we can capture the Exception or error when it is occurred and act accordingly. ie., displaying a Error page, saying that

    "Sorry! Your request cannot be processed at this moment, Please try again after some time"


There are two ways of handling the exceptions:


  • Structured Exception Handling

  • UnStructured Exception Handling

First, lets have a quick look at the Unstructured Exception Handling.

Unstructed Exception Handling

Unstructed exception handling is carried out using the On Error Statement. The On Error statement handles any error that occurs in that block of code and transfer the control to the line label.

Syntax

On Error GoTo <line Label>
or
On error Resume Next (This statement is used to specify that when an error occurs, the control should pass to the next line of code followin the line in which the error occured.)

Note: When an error occurs in a procedure which doesn't have any On Error or Try ..... Catch ... Finally block, the exception or error is passed back to the calling procedure and the error is handled in the calling procedure.

Example of Unstructed exception handling

Public Function unstructeddivide( ByVal a As Integer, ByVal b As Integer) As Integer
On Error Resume Next

Return a / b
End Function

The above code snippet has a chance of raising an error if we pass 0 for the second value, so i have used to on error resume next statement, which ignores the error and proceeds with the next statement.

Syntax/Format of Unstructured exception handling

Public sub FunctionName()
On Error GoTo LineLabel

'the block of code which may raise an error
Exit Sub
LineLabel:

'The block of code which is executed when error occurs.
Resume
End Sub


Explanation

When an error occurs in the code wrriten in the On Error GoTo block, the control passes to the error handler LineLabel. Then the code in the error handling block is executed, When the control is transferred to the Resume statement, control return back to the line of code in which the error
occured.

The Exit Sub statement should be placed immediately before the error handler block. If the Exit Sub statement is not written, the error handler code is executed even if there is no error/exception condition giving unexpected results.

Public Function unstructeddivide(ByVal a As Integer, ByVal b As Integer) As Integer
On Error GoTo linelab1

unstructeddivide = (a / b)
Exit Function

linelab1:
MsgBox("Divide by zero not allowed")
Resume Next
End Function


Note:

  • If there is any probability for an error to raise in a function, it is
    better to call On Error Resume Next statement.

  • On Error GoTo 0 statement turns
    off the process of error trapping and disables the error handler.


Conclusion

Unstructured exception handling is not recommended, since the code written
using On Error Statement is very difficult to maintain. Better
stick with Structed Exception Handling.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Multi Threading in Vb.Net
Previous Resource: Working with Exceptions - Part II (Structured Exception Handling).
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use