473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

System.DivideByZeroException does not catch exception

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
Nov 21 '05 #1
2 3972
But System.DivideByZeroException does not catch the exception. If I use
System.OverflowException it will work.
What's the reason?


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.
Nov 21 '05 #2
Thanks Mattias!
It was great tip :-)
Amin

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:u#**************@TK2MSFTNGP09.phx.gbl...
But System.DivideByZeroException does not catch the exception. If I use
System.OverflowException it will work.
What's the reason?


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.

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
20
by: djc | last post by:
I get this *intermittently* on a utility I am working on. I don't know whats going on but here are a few points about it: - using VS 2005, running on xp sp2 - program uses multiple threadpool...
2
by: koredump | last post by:
Hi all, I have a windows app that makes some asyc calls to my webservice (WSE 3.0 with MTOM). exception gets thrown in the client win app. This exception is being thrown on another thread by a...
5
by: scheidel21 | last post by:
I am writting a DB front end in VB.NET 2003 I have a form that displays employee information it has a dataset loaded from the DB that also includes tables for lookup in some comboboxes that are...
2
by: astolpho | last post by:
I am using a slightly outdated reference book on J2EE programming. It gives 2 methods of creating a database used in its casestudies. The first is an ANT script that gives the following output: ...
4
by: Brandon McCombs | last post by:
Hello, From my understanding, DirectoryEntry is used to connect to Active Directory. Although this makes no sense whatsoever I accept it. The problem is how am I supposed to test whether a...
2
by: Carol | last post by:
Exception may be thrown in the code inside the try block. I want to handling the SqlException with State == 1 in a special way, and for all others I want to use a general way to handle. Which of...
5
by: Henry Stock | last post by:
I am trying to understand the following error: Any thing you can tell me about this is appreciated. Security Exception Description: The application attempted to perform an operation not allowed...
30
by: Yorian | last post by:
Hey, Although I've been using classes and object for quite a while now I've never actually programmed proper OO code yet. It ofcourse depends on what you call proper OO code. I have been...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.