 |
|
|
| | | | | | | | | | | |
| | | | | | | | | | |
|
|
| Advertisements |
Ask Questions, Share Knowledge with Krify Answers |
|
|
Posted by:
Deepak L M Article
viewed:
476 times |
vb and vb.net exceptional handling
|
|
Difference between old Visual Basic "On Error" and new VB.NET exception handling
If you are coming from Visual Badic background, you will find that the error handling mechanism is totally changed in VB.NET.
Visual Basic provides an error handling mechanism, now called un
structured error handling . Typically, you will place a code statement
"On Error Goto ErrorHandler" on top of your code block. This means,
when an error occurs, jump to the path specified by Errorhandler . See
the following sample code :
Sub MyMethod
On Error GoTo ErrorHandler
code that may raise an error
Exit Sub
ErrorHandler:
Error handling code
Resume
End Sub
In the above sample code, if an error occurs in the method, the program
execution will jump to the ErrorHandler block. This saves your program
from terminating abnormally and gives an opportunity to log the error
details and show friendly message to the user.
Equivalent of On Error in VB.NET and C#
Many programmers coming from Visual Basic to .NET asks this question :
"what is the equivalent of On Error in .NET ?". The answer is, there is
no direct equivalent. Just for backward compatibility, VB.NET supports
the "On Error" statement. This is not available in C#. Even though "On
Error" statement is supported in VB.NET, you are strongly encouraged to
use the new structured exception handling mechanism provided by the
.NET Framework.
The above sample code can be re written in VB.NET as shown below :
Private Sub MyMethod()
Try
code that can raise an error
Catch ex As Exception
code to handle the exception.
End Try
End Sub
|
Disclaimer: The above article is responsible of the individual who post, krify.com does not hold responsible for any kind of
disinformation.
If you discover one or more of the krify.com pages direct you to
messages that harass, abuse, have obscene, unlawful, defamatory,
libellous, hateful, or otherwise objectionable content; or have spam,
please inform to krify.com and that will be deleted as soon as
possible.
|
|
|
|
|