Saturday, October 27, 2007

Exception Handling of VB.NET & C#

Exception Handling

In vb.net & c# language Exception Handling are differ.Code Exception Handling of vb.net & c# language.

____________________________________________________________________

VB.NET

Throw an exception
Dim ex As New Exception("Something is really wrong.")
Throw ex

' Catch an exception
Try
y = 0
x = 10 / y
Catch ex As Exception When y = 0 ' Argument and When is optional
Console.WriteLine(ex.Message)
Finally
Beep()
End Try

' Deprecated unstructured error handling
On Error GoTo MyErrorHandler
...
MyErrorHandler: Console.WriteLine(Err.Description)

-------------------------------------------------------------------------------------------------

C#

// Throw an exception
Exception up = new Exception("Something is really wrong.");
throw up; // ha ha

// Catch an exception
try {
y = 0;
x = 10 / y;
}
catch (Exception ex) { // Argument is optional, no "When" keyword
Console.WriteLine(ex.Message);
}
finally {
// Requires reference to the Microsoft.VisualBasic.dll
// assembly (pre .NET Framework v2.0)
Microsoft.VisualBasic.Interaction.Beep();
}

-------------------------------------------------------------------------------------------------

Note: This code applicable for both desktop and web application.

-------------------------------------------------------------------------------------------------

No comments: