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?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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 |
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |