System.DivideByZeroException does not catch exception
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.vb.

Post a new message to this list...

Amin Sobati
Hi,
I have the following code in my app:

Dim intx As Integer
Dim inty As Integer
Dim intz As Integer
inty = 0
intx = 5
Try
intz = intx / inty
Catch ex As System.DivideByZeroException
MsgBox("Catch")
End Try

But System.DivideByZeroException does not catch the exception. If I use
System.OverflowException it will work.
What's the reason?
Thanks,
Amin

Reply to this message...
 
    
Mattias Sjögren
[Original message clipped]

The reason is that the / operator performs floating point division,
which doesn't throw a DivideByZeroException but rather returns NaN or
Infinity. If you turn on Option Strict this whould be more visible
since converting the result back to an Integer is a narrowing
conversion.

The solution may be to use the integer division operator \ instead.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Reply to this message...
 
    
Amin Sobati
Thanks Mattias!
It was great tip :-)
Amin

"Mattias Sjögren" <Click here to reveal e-mail address> wrote in message
news:u#Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.DivideByZeroException
System.OverflowException




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com