The block of code in C# will produce a
DivideByZeroException, as one would think by looking at it:
CODE
private void DivideNumbers()
{
try
{
int num1 = 10;
int num2 = 0;
int num3 = num1 / num2;
}
catch(DivideByZeroException ex)
{
MessageBox.Show(ex.Message);
}
}
Byt the same exact code in VB.Net will produce an
OverflowException.
CODE
Private Sub DivideNumbers()
Try
Dim num1 As Integer = 10
Dim num2 As Integer = 0
Dim num3 As Integer = num1 / num2
Catch ex As DivideByZeroException
MessageBox.Show(ex.Message)
End Try
End Sub
Now it took me some time to figure out how to make the VB.Net version to cause the DivideByZeroException I was expecting, but I still cannot, and neither can any of the geeks I work with, figure out why it works in C# but not VB.Net. When you use the
/ division operator in VB.Net it doesn't actually return a divide by zero problem, it returns NaN, or an infinity value, thus the
OverflowException.
To change the VB.Net version to raise the
DivideByZeroException as you want, us the whole number division operator
\ like so:
CODE
Private Sub DivideNumbers()
Try
Dim num1 As Integer = 10
Dim num2 As Integer = 0
Dim num3 As Integer = num1 \ num2
Catch ex As DivideByZeroException
MessageBox.Show(ex.Message)
End Try
End Sub
Now I know this sounds useless to most, but I am currently working on a custom exception handling object, and I really have a pet peeve of
catch(Exception ex), look for the exception you think will occur. so while testing my exception object I wanted to raise a
DivisionByZeroException (among other exceptions) to ensure the object works perfectly. Interesting to me anyways

PS: There will be a tutorial on the Custom Exception Handler showrtly
This post has been edited by PsychoCoder: 29 Nov, 2007 - 10:52 PM