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 II (Structured Exception Handling).


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



What we saw in the Part I

We defined the exception, then we discussed in detail about the Unstructured Exception handling.

Introduction

The application is divided into blocks of code, those blocks that have the probability of raising an error contain one or more associated exception handlers. Structured Exception handler is very simple, it is very similar to the if .... else if .... End If or
Select Case
statements.

Structured Exception handling is done using Try....Catch....Finally statement.

Try....Catch....Finally Statement

When an application runs, the code in the Try block is executed first, if the code gives any error, the error is handled by the catch block. We can have multiple catch block in the Try statement. (Because, our code may raise different exception on different situation.)

For example, Let us assume that we are doing both file operation and database operation, there are changes for getting the IOExceptions, File Exceptions and Database exceptions. We need to handle all the exceptions, ie., we need to be prepared for everything (Whatever be the exception) our code should be capable of handling them.

Syntax for Try....catch statement

Try ' Block of code, where the chances of getting exception is there
catch <Exception object< 'Block of code that nneds to be executed when the exception of that type is caught
Finally 'Block of code that needs to be executed, even when there is no exceptions. End Try

Finally block is executed just before exists the Try...Catch...Finally back in which the error occured. Finaly block is executed even when the exception is not raised.

Finally block is very useful. For example, we are doing some file operations, where i have opened a file, if the file is opened, it will locked for editing the file, lock is removed only when we close the file. So we need to close the file even if the exception is raised or not.

Example for Structured Exception Handling

Public Function Structeddivide(ByVal a As Integer, ByVal b As Integer) As Integer
Try
Return a / b
Catch ex As OverflowException
MsgBox("Cannot divide by zero")
Return -1
Finally
MsgBox("Calculation over")
End Try
End Function


The above code when 0 is passed as the second parameter and when we try to execute this method it raises an OverFlowException, since any number divided by Zero will result in NaN.

Example for Catching Multiple Exceptions

Public Function Structeddivide(ByVal a As Integer, ByVal b As Integer) As Integer
Dim fis1 As System.IO.FileStream
Dim fw As System.IO.StreamWriter
Dim fis2 As System.IO.FileStream
Dim fr As System.IO.StreamReader
Try
fis1 = New System.IO.FileStream("c:\WriteResult.txt", IO.FileMode.Append)
fw = New System.IO.StreamWriter(fis1)
fw.WriteLine(a / b)
fis1 = New System.IO.FileStream("c:\WriteResult.txt", IO.FileMode.Append)
fr = New System.IO.StreamReader(fis1)
MsgBox(fr.ReadLine)
Return a / b
Catch ioexc As System.IO.IOException
MsgBox("IO Exception Occured")
Catch ex As OverflowException
MsgBox("Cannot divide by zero")
Return -1
Finally
MsgBox("Calculation over")
fw.Close()
fis1.Close()
End Try
End Function


When an error occurs in the Try block code, the control automatically transfers to the first try catch block, If the Exception matches with the exception handled in the catch block executes that block of code or else skips to the next catch block. If no matching catch block is found the control is transferred to the function or the link where this function was actually called. Regardless of whichever Catch block is executed, the code in the Finally block is executed before exiting the Try block.

Some Properties of Exception Class

HelpLink : Help File associated with the exception
Message : Read Only Property, Which gives the description of the exception raised
Source : The name of the application or object which caused the exception
StackTrace : Gives the full history of the exception, like where the exception is thrown, why it is thrown etc.,

We will see more about Nested Exceptions, creating user defined exceptions in the next section of this article




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: Working with Exceptions - Part I (Unstructed Exception Handling).
Previous Resource: Working with Exceptions - Part III (Nested Try....Catch....Block)
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