473,796 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about exceptions

Hi everybody,
I was testing a piece of code and I noticed that try catch block doesn't
recognize "DivideByZeroEx ception" when I have zero in denominator and I have
to use the "OverflowExcept ion" , I can't figure out why VB behaves this way ,
Any thoughts?
--
Best regards,
Edward
Jun 27 '08 #1
7 1011
On 2008-05-30, Edward <Ed****@discuss ions.microsoft. comwrote:
Hi everybody,
I was testing a piece of code and I noticed that try catch block doesn't
recognize "DivideByZeroEx ception" when I have zero in denominator and I have
to use the "OverflowExcept ion" , I can't figure out why VB behaves this way ,
Any thoughts?
You probably need to read the documentation on the DivideByZeroExc eption
:) See, this one is only thrown by integer and decimal types. Floating
point numbers, single and double do not throw this exception - they
return NaN.

Now, you are probably going to say that you are using integers - but,
probably not... See VB (both classic and .NET) have two division
operators / and \. The commonly used / actually performs it's work by
converting it's operands to double values - so it won't throw this
exception. I would bet that if you switched to \ instead - doing
integer division that it would throw a DivideByZeroExc eption... Here
let me try it real quick :)

Yep... In fact, if you do say 3\0 using literals - it won't even
compile :) But if you do it with variables, then a
DivideByZeroExc eption is thrown at runtime.

--
Tom Shelton
Jun 27 '08 #2
"Edward" <Ed****@discuss ions.microsoft. comschrieb
Hi everybody,
I was testing a piece of code and I noticed that try catch block
doesn't recognize "DivideByZeroEx ception" when I have zero in
denominator and I have to use the "OverflowExcept ion" , I can't
figure out why VB behaves this way , Any thoughts?
Can't repro this. Can you show some code? Do you have Option Strict
enabled? I guess you try to assign the result of a floating point
division to an Integer value. So, not the division is the problem, the
assignment is. We must distinguish between floating point division and
integer division. The former does not throw an exception if the
demoninator is 0. Instead the result is PositiveInfinit y,
NegativeInfinit y or NaN (see help for / operator). The latter throws a
DivideByZeroExc eption.
Armin

Jun 27 '08 #3
On 2008-05-30, Armin Zingler <az*******@free net.dewrote:
"Edward" <Ed****@discuss ions.microsoft. comschrieb
>Hi everybody,
I was testing a piece of code and I noticed that try catch block
doesn't recognize "DivideByZeroEx ception" when I have zero in
denominator and I have to use the "OverflowExcept ion" , I can't
figure out why VB behaves this way , Any thoughts?

Can't repro this. Can you show some code? Do you have Option Strict
enabled? I guess you try to assign the result of a floating point
division to an Integer value. So, not the division is the problem, the
assignment is. We must distinguish between floating point division and
integer division. The former does not throw an exception if the
demoninator is 0. Instead the result is PositiveInfinit y,
NegativeInfinit y or NaN (see help for / operator). The latter throws a
DivideByZeroExc eption.
Armin
/ converts its operands to floating point values - so does not throw a
DivisionByZeroE xception. You need to be doing integer division (\) to
get that exception.

--
Tom Shelton
Jun 27 '08 #4
Both of you guys were right ! I wasn't aware of integer division !
--
Best regards,
Edward
"Tom Shelton" wrote:
On 2008-05-30, Armin Zingler <az*******@free net.dewrote:
"Edward" <Ed****@discuss ions.microsoft. comschrieb
Hi everybody,
I was testing a piece of code and I noticed that try catch block
doesn't recognize "DivideByZeroEx ception" when I have zero in
denominator and I have to use the "OverflowExcept ion" , I can't
figure out why VB behaves this way , Any thoughts?
Can't repro this. Can you show some code? Do you have Option Strict
enabled? I guess you try to assign the result of a floating point
division to an Integer value. So, not the division is the problem, the
assignment is. We must distinguish between floating point division and
integer division. The former does not throw an exception if the
demoninator is 0. Instead the result is PositiveInfinit y,
NegativeInfinit y or NaN (see help for / operator). The latter throws a
DivideByZeroExc eption.
Armin

/ converts its operands to floating point values - so does not throw a
DivisionByZeroE xception. You need to be doing integer division (\) to
get that exception.

--
Tom Shelton
Jun 27 '08 #5
Tom,

I think it depends on the operand types. For example, this function will
throw a DivideByZero exception if donors is zero:

Private Function getAverage(ByVa l donations As Decimal, ByVal donors As
Integer) As Decimal

'Calculate and return the average donation
getAverage = donations / donors

End Function

Kerry Moorman
"Tom Shelton" wrote:
>
/ converts its operands to floating point values - so does not throw a
DivisionByZeroE xception. You need to be doing integer division (\) to
get that exception.

--
Tom Shelton
Jun 27 '08 #6
"Tom Shelton" <to*********@YO UKNOWTHEDRILLco mcast.netschrie b

Can't repro this. Can you show some code? Do you have Option
Strict enabled? I guess you try to assign the result of a floating
point division to an Integer value. So, not the division is the
problem, the assignment is. We must distinguish between floating
point division and integer division. The former does not throw an
exception if the demoninator is 0. Instead the result is
PositiveInfinit y,
NegativeInfinit y or NaN (see help for / operator). The latter
throws a DivideByZeroExc eption.

/ converts its operands to floating point values - so does not throw
a DivisionByZeroE xception. You need to be doing integer division
(\) to get that exception.
Yep, that's what I wrote.
Armin
Jun 27 '08 #7
On May 30, 4:59*pm, Kerry Moorman
<KerryMoor...@d iscussions.micr osoft.comwrote:
Tom,

I think it depends on the operand types. For example, this function will
throw a DivideByZero exception if donors is zero:

* * Private Function getAverage(ByVa l donations As Decimal, ByVal donors As
Integer) As Decimal

* * * * 'Calculate and return the average donation
* * * * getAverage = donations / donors

* * End Function

Kerry Moorman
Hmm... Actually, that's probably correct in this case. Since
decimals can hold a bigger range of numbers, the / operator probably
does use decimal in this case - and decimal is really an integer type
under the covers, so it throws the DivideByZeroExc eption. Not one I
thought about since, VB.CLASSIC didn't have a real decimal type (it
was a variant sub type)....

Thanks for that, Kerry.

--
Tom Shelton
Jun 27 '08 #8

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

Similar topics

3
1271
by: Zachary | last post by:
Hello, I'm relatively new at Python, and am slightly confused about how the try...except mechanism is supposed to work. I am just not sure how it would be used, as it just seems that it is another sort of loop. Is their any sort of example I could look at, that has an example of how it is applied? I haven't really seen any such full example. Thanks, And Sorry for the newbie Question, Zack
6
1798
by: Flare | last post by:
Hi i just read: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/exceptdotnet.asp Wich is interesting reading by the way. But. I have'nt used exception very much to anything else than ordinary FileExeptions etc. Now i want to create my own exception hierarchy. I want to do this so that i can log the exceltion information to a file or a database. Doesent mather right now.
9
2538
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in the way of CPU usage), compared to the "normal" practise returning values. How true is this? Will using using exception handling, in general, be much less efficient than returning values, or less efficient at all? Just curious...
5
1952
by: John Richardson | last post by:
Quick question about the UnhandledException event and associated Handler. I just implemented this handler for the first time, and am surprised that it this event is being raised for an exception that I have handled. I have the following: private void Load() { try {
10
1511
by: tony | last post by:
Hello!! As you know every user defined exception must be derived from class Exception. Now to my question if I write catch then every exception will be caught. If I instead write catch(Exception) then every exception will also be caught. These two will catch the exact same exception is this right understod?
53
3195
by: jaso | last post by:
Can you give any comments on this code? I used one goto, is it bad? #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <assert.h> #define NOT_NULL 1
4
1855
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and it has now, after 15k lines of code resulted in a royal mess! It's my hope to ask some specific questions with scenario examples and that some of you might offer a little guidance or general suggestions. 1) string...
2
1205
by: pack | last post by:
"Unless you have a very good reason to catch an exception, DON'T. Exceptions are supposed to be exceptional, just like the dictionary meaning: uncommon, unusual. When in doubt, let the calling routine, or the global exception handler, deal with it. This is the golden rule. The hardest kinds of exceptions to troubleshoot are the ones that don't even exist, because a developer upstream of you decided to consume it." What's "global...
15
1901
by: =?Utf-8?B?TWljaGVsIFBvc3NldGggW01DUF0=?= | last post by:
In my opinion rethrowing exceptions without providing anny extra information is a totall waste Examples : in my opinion wrong : A: Public sub DoSomeStuff() Try do it
6
1528
by: rhaazy | last post by:
I am looking for some feedback on using try catch statements. Usually when I start a project I use them for everything, but stop using them as often after the "meat n' potatos" of the project is finished. I am thinking it would be useful to at least have a blanket try-catch to surround all of the code, and add more to aid in debugging and catching specific exceptions. I want to change my habits but before I do I wanted to see if anyone...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.