Welcome to Dream.In.Code
Getting Help is Easy!

Join 135,935 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,654 people online right now. Registration is fast and FREE... Join Now!




Difference between how C# and VB.Net handle division

 
Reply to this topicStart new topic

Difference between how C# and VB.Net handle division

PsychoCoder
29 Nov, 2007 - 10:13 PM
Post #1

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8525
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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 smile.gif

PS: There will be a tutorial on the Custom Exception Handler showrtly smile.gif

This post has been edited by PsychoCoder: 29 Nov, 2007 - 10:52 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Difference Between How C# And VB.Net Handle Division
29 Nov, 2007 - 10:23 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,173



Thanked: 208 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
That silly NaN and Infinity value, trix are for kids! wink2.gif
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Difference Between How C# And VB.Net Handle Division
29 Nov, 2007 - 11:18 PM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,907



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
It is a very interesting, I discovered the difference some time ago and have always wondered why that is. As of today, I still have not found a satisfactory answer.

And to make it even more interesting, depending on the data type of the variables you are dividing, you will get different results.

For example, if you use type Double it will not throw any errors, but instead will execute and store "Infinity" in the num3 variable.
CODE

    Private Sub DivideNumbers()
        Try
            Dim num1 As Double = 10
            Dim num2 As Double = 0
            Dim num3 As Double = num1 / num2
            MessageBox.Show(num3)
        Catch ex As DivideByZeroException
            MessageBox.Show(ex.Message)
        End Try
    End Sub


Decimal will throw a DivideByZeroException.
Long will throw a OverflowException.
Single will not throw an exception, but will store "Infinity" in the variable.

However, using the whole number divisor all data types will result in a DivideByZeroException.

Where is the consistency in it all?!? smile.gif
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Difference Between How C# And VB.Net Handle Division
29 Nov, 2007 - 11:27 PM
Post #4

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
QUOTE(Martyr2 @ 30 Nov, 2007 - 11:53 AM) *

trix are for kids! wink2.gif

I still like it tongue.gif
User is online!Profile CardPM
+Quote Post

born2c0de
RE: Difference Between How C# And VB.Net Handle Division
30 Nov, 2007 - 02:16 AM
Post #5

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,906



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Oh, so it's an Overflow exception.
I used to wonder why it wasn't DivideByZero in VB.NET

I used to use cheap quick-fixes like this before:
CODE
Private Sub DivideNumbers()
    Try
        Dim num1 As Integer = 10
        Dim num2 As Integer = 0
        Dim num3 As Integer = num1 / num2
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Luckily in such cases, since the only exception that can occur is the division by zero error, there was no need to worry about catching other exceptions as well.

Thanks man. Cheers!!!
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Difference Between How C# And VB.Net Handle Division
30 Nov, 2007 - 04:52 AM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8525
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
QUOTE(jayman9 @ 29 Nov, 2007 - 11:18 PM) *

Where is the consistency in it all?!? smile.gif


And why does it work in C# perfectly with he / division operator? That's a answer I haven't found yet.

This post has been edited by PsychoCoder: 30 Nov, 2007 - 04:53 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:31AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month