School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Difference between how C# and VB.Net handle division

#1 PsychoCoder  Icon User is offline

  • iHater.Init(this);
  • Icon

Reputation: 1364
  • View blog
  • Posts: 19,696
  • Joined: 26-July 07


Dream Kudos: 12925

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

Share |

Difference between how C# and VB.Net handle division

Post icon  Posted 29 November 2007 - 10:13 PM

The block of code in C# will produce a DivideByZeroException, as one would think by looking at it:

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.

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:

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 November 2007 - 10:52 PM

Was This Post Helpful? 0
  • +
  • -


#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon

Reputation: 1336
  • View blog
  • Posts: 8,175
  • Joined: 18-April 07


Dream Kudos: 0

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

Re: Difference between how C# and VB.Net handle division

Posted 29 November 2007 - 10:23 PM

That silly NaN and Infinity value, trix are for kids! ;)
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • Icon

Reputation: 345
  • View blog
  • Posts: 9,235
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Re: Difference between how C# and VB.Net handle division

Posted 29 November 2007 - 11:18 PM

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.
	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?!? :)
Was This Post Helpful? 0
  • +
  • -

#4 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • Icon

Reputation: 10
  • View blog
  • Posts: 1,953
  • Joined: 03-August 06


Dream Kudos: 755

Re: Difference between how C# and VB.Net handle division

Posted 29 November 2007 - 11:27 PM

View PostMartyr2, on 30 Nov, 2007 - 11:53 AM, said:

trix are for kids! ;)

I still like it :P
Was This Post Helpful? 0
  • +
  • -

#5 born2c0de  Icon User is offline

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

Reputation: 137
  • View blog
  • Posts: 4,622
  • Joined: 26-November 04


Dream Kudos: 2825

Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

Re: Difference between how C# and VB.Net handle division

Posted 30 November 2007 - 02:16 AM

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:
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!!!
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • iHater.Init(this);
  • Icon

Reputation: 1364
  • View blog
  • Posts: 19,696
  • Joined: 26-July 07


Dream Kudos: 12925

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

Re: Difference between how C# and VB.Net handle division

Posted 30 November 2007 - 04:52 AM

View Postjayman9, on 29 Nov, 2007 - 11:18 PM, said:

Where is the consistency in it all?!? :)


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 November 2007 - 04:53 AM

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users