IT Community - Software Programming, Web Development and Technical Support

How can we Handle Exception in VB.Net?

This is a discussion on How can we Handle Exception in VB.Net? within the VB.NET Programming forums, part of the Software Development category; How can we Handle Exception in VB.Net?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > VB.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-26-2007, 05:42 AM
anbuchezhians anbuchezhians is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 77
anbuchezhians is on a distinguished road
Question How can we Handle Exception in VB.Net?

How can we Handle Exception in VB.Net?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-27-2007, 02:49 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Thumbs up Re: How can we Handle Exception in VB.Net?

Structured exception handling using Try....Catch.....Finally

The general form try-catch-finally in VB.NET is shown below.

Try
' Statement which can cause an exception.
Catch x As Type
' Statements for handling the exception
Finally
End Try 'Any cleanup code
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-10-2007, 05:19 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Default Re: How can we Handle Exception in VB.Net?

Exception handling is an in built mechanism in .NET framework to detect and handle run time errors. The .NET framework contains lots of standard exceptions. The exceptions are anomalies that occur during the execution of a program. They can be because of user, logic or system errors. If a user (programmer) do not provide a mechanism to handle these anomalies, the .NET run time environment provide a default mechanism, which terminates the program execution.

VB.NET provides three keywords try, catch and finally to do exception handling. The try encloses the statements that might throw an exception whereas catch handles an exception if one exists. The finally can be used for doing any clean up process.

The general form try-catch-finally in VB.NET is shown below.

Try
' Statement which can cause an exception.
Catch x As Type
' Statements for handling the exception
Finally
End Try 'Any cleanup code


If any exception occurs inside the try block, the control transfers to the appropriate catch block and later to the finally block.

But in VB.NET, both catch and finally blocks are optional. The try block can exist either with one or more catch blocks or a finally block or with both catch and finally blocks.

If there is no exception occurred inside the try block, the control directly transfers to finally block. We can say that the statements inside the finally block is executed always. Note that it is an error to transfer control out of a finally block by using break, continue, return or goto.

In VB.NET, exceptions are nothing but objects of the type Exception. The Exception is the ultimate base class for any exceptions in VB.NET. The VB.NET itself provides couple of standard exceptions. Or even the user can create their own exception classes, provided that this should inherit from either Exception class or one of the standard derived classes of Exception class like DivideByZeroExcpetion ot ArgumentException etc.

Example:


//VB.NET: Exception Handling
Imports System
Class MyClient
Public Shared Sub Main()
Dim x As Integer = 0
Dim div As Integer = 0
Try
div = 100 / x
Console.WriteLine("Not executed line")
Catch de As DivideByZeroException
Console.WriteLine("Exception occured")
Finally
Console.WriteLine("Finally Block")
End Try
Console.WriteLine("Result is {0}", div)
End Sub 'Main
End Class 'MyClient
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How does JSP handle run-time exceptions? leoraja8 Java Server Pages (JSP) 3 03-20-2008 09:39 PM
How will ASP.NET handle session management? prasath ASP and ASP.NET Programming 1 07-19-2007 02:53 AM
Handle Case Sensitive Search in MySQL priyan Database Support 6 07-10-2007 07:21 AM
Photo Sharing Even the Folks Can Handle vigneshgets The Lounge 0 06-06-2007 01:06 AM
How do you handle this type of situations vadivelanvaidyanathan Software Testing 0 04-06-2007 11:19 PM


All times are GMT -7. The time now is 11:45 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0