Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
NaN, Infinity, and Divide by Zero
New VB.NET Constants and Structured Error Handling.
 Topics
  From The Article
MSDN on
Structured Error Handling

 

Most introductory texts about VB 6 and earlier include this warning: Don't divide by zero! You'll get a runtime error!

But things have changed in VB.NET! And, although there are more programming options, and the calculation is actually more accurate, it isn't always easy to see why things happen the way they do. We cover the new VB.NET constants: NaN, and Infinity. And we show how to handle division by zero using VB.NET's structured error handling!

Here's an example of divide by zero in VB 6!

VB6 Divide by Zero

But if you run exactly the same program in VB.NET, look what happens!

VB.NET Divide by Zero

So what's going on here? The answer is that VB.NET actually gives you the mathematically correct answer. Mathematically, you CAN divide by zero, but what you get is "infinity". This isn't too useful for business applications (unless the CEO is wondering what the upper limit on his stock bonus is). But it does keep your applications from crashing on a runtime exception like VB 6 does. And VB.NET gives you plenty of rope by even allowing you to perform calculations. Check this out:

Infinite Calculations

If you turn on Option Strict, what's happening in the code becomes easier to see since converting the result back to an Integer is a narrowing conversion that Option Strict doesn't allow.

Option Strict Double Objects

VB.NET also gives you the answer NaN (Not a Number) for some calculations such as 0 / 0 and distinguishes between positive infinity and negative infinity ( -1 / 0 ). One way to see the options that VB.NET offers is to use VB.NET's Intellisense to view the objects available.

Keep in mind that all of these new capabilities of VB.NET are only available with floating point (Double or Single) data types. And this flexibility can lead to some Try-Catch-Finally (structured error handling) confusion. For example, the .NET code above runs without throwing any kind of exception, so coding it inside a Try-Catch-Finally block won't help. (You would have to code a test like this: If c.ToString = "Infinity" Then ...).

Even if you code the program (using Integer instead of Single or Double types), you still get an "Overflow" Exception, not a "Divide by Zero" exception. If you search the web for other technical help, you will notice that the examples all test for OverflowException.

Overflow Exception

.NET actually has the DivideByZeroException as a legitimate type. How can you trigger this elusive error in a Try-Catch-Finally block?

As it turns out, Microsoft's MSDN page about Try-Catch-Finally blocks actually uses a divide by zero example to illustrate how to code them. But there's a subtle "catch" that they don't explain on their page. Their code looks like this:

Dim a As Integer = 0
Dim b As Integer = 0
Dim c As Integer = 0

Try
  a = b \ c
Catch exc As Exception
  Console.WriteLine("A run-time error occurred")
Finally
  Console.ReadLine()
End Try

And it DOES trigger an actual divide by zero exception.

Divide By Zero

So why does this get a divide by zero and nothing else does? And what is Microsoft not explaining?

Notice that the operation they use is NOT divide ("/"), it's integer divide ("\")! As it turns out, this is the only case that actually throws that exception. It would have been nice if Microsoft (and the other pages that copied their code) explained that little detail!

Explore Visual Basic

By Category

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.