473,386 Members | 1,766 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,386 software developers and data experts.

Exceptions are not raised

Hi to everyone. I am studying VB.net and I am wondering why this trivial
code doesn't raise any exception:

Option Strict On

Imports System
Imports Microsoft.VisualBasic
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim num1, num2 As Short
Dim num3 As Double
num1 = 5
num2 = 0
Try
num3 = num1 / num2
Catch ex As DivideByZeroException
MsgBox("Divisione per 0!")
End Try
End Sub

End Class

No msgbox is executed!!!
I'm using VB 2005 Express Edition.
Should I set anything in the framework or project properties?
Thanks in advance to everyone.

Giuseppe from Italy.
Apr 5 '06 #1
4 1345
Exceptions certainly *are* raised when they occur. However, in this case,
you're expecting a "divide by zero" exception, but it will not occur,
because in the .Net Platform, dividing by zero is not an illegal operation.
It results in "infinity."

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"Giuseppe Chielli" <gi******@NOSPAMlibero.it> wrote in message
news:mM*********************@twister2.libero.it...
Hi to everyone. I am studying VB.net and I am wondering why this trivial
code doesn't raise any exception:

Option Strict On

Imports System
Imports Microsoft.VisualBasic
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim num1, num2 As Short
Dim num3 As Double
num1 = 5
num2 = 0
Try
num3 = num1 / num2
Catch ex As DivideByZeroException
MsgBox("Divisione per 0!")
End Try
End Sub

End Class

No msgbox is executed!!!
I'm using VB 2005 Express Edition.
Should I set anything in the framework or project properties?
Thanks in advance to everyone.

Giuseppe from Italy.

Apr 5 '06 #2
> Hi to everyone. I am studying VB.net and I am wondering why this trivial
code doesn't raise any exception:
Dim num1, num2 As Short
Dim num3 As Double
num1 = 5
num2 = 0
Try
num3 = num1 / num2
Catch ex As DivideByZeroException
MsgBox("Divisione per 0!")
End Try


Divide by zero is for integral types. Change / to \ and you should get an
exception. When you use /, integral num1 and num2 are cast to floating
point, and since num3 is double, no exception. Cint(num1 / num2) would also
generate an exception (not divide by zero, don't remember exactly what).

Apr 5 '06 #3
Hello, Giuseppe,

To expand a little on the previous posts, .Net sets the floating point
control register in such a way that this operation, and a host of
others, do not signal an error. Instead of an error, a "special"
floating point value is returned. The possible return values (for
Double) are:

Double.NaN()
Double.NegativeInfinity()
Double.PositiveInfinity()

You can test for these results by using the functions:

Double.IsInfinity()
Double.IsNaN()
Double.IsNegativeInfinity()
Double.IsPositiveInfinity()

(There are corresponding values and functions for Single.)

I don't know if the floating point control register can be controlled
directly in .Net, but other languages allow this. So it IS possible to
change the behaviour so that floating point errors will raise exceptions
by calling an unmanaged routine. However (since other .Net code will
not be expecting it) it is probably not a good idea in general. If you
were to do this however, I think that the exception raised will be
ArithmeticException, not DivideByZeroException, which (as was pointed
out) is for Integral data types.

Cheers,
Randy
Giuseppe Chielli wrote:
Hi to everyone. I am studying VB.net and I am wondering why this trivial
code doesn't raise any exception:

Option Strict On

Imports System
Imports Microsoft.VisualBasic
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim num1, num2 As Short
Dim num3 As Double
num1 = 5
num2 = 0
Try
num3 = num1 / num2
Catch ex As DivideByZeroException
MsgBox("Divisione per 0!")
End Try
End Sub

End Class

No msgbox is executed!!!
I'm using VB 2005 Express Edition.
Should I set anything in the framework or project properties?
Thanks in advance to everyone.

Giuseppe from Italy.

Apr 5 '06 #4
R. MacDonald wrote:
Hello, Giuseppe,

To expand a little on the previous posts, .Net sets the floating point
control register in such a way that this operation, and a host of
others, do not signal an error. Instead of an error, a "special"
floating point value is returned. The possible return values (for
Double) are:

Double.NaN()
Double.NegativeInfinity()
Double.PositiveInfinity()

You can test for these results by using the functions:

Double.IsInfinity()
Double.IsNaN()
Double.IsNegativeInfinity()
Double.IsPositiveInfinity()

(There are corresponding values and functions for Single.)

I don't know if the floating point control register can be controlled
directly in .Net, but other languages allow this. So it IS possible to
change the behaviour so that floating point errors will raise exceptions
by calling an unmanaged routine. However (since other .Net code will
not be expecting it) it is probably not a good idea in general. If you
were to do this however, I think that the exception raised will be
ArithmeticException, not DivideByZeroException, which (as was pointed
out) is for Integral data types.
Cheers,
Randy

Thanks a lot for you explanation.That's very useful for me!!!
Thanks a lot.
Apr 5 '06 #5

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

Similar topics

1
by: Vegard Bakke | last post by:
From whet I can see, Python documentation is lacking a very important piece of information. Very few functions have documented what exceptions they raise, and under what cicumstances. Is this a...
0
by: seth | last post by:
Last week I encountered an AttributeError in my unit tests that I wasn'table to catch with an "except AttributeError" statement. The problem stemmed from a class that raised an error inside...
33
by: Steven Bethard | last post by:
I feel like this has probably been answered before, but I couldn't find something quite like it in the archives. Feel free to point me somewhere if you know where this has already been answered. ...
21
by: dkcpub | last post by:
I'm very new to Python, but I couldn't find anything in the docs or faq about this. And I fished around in the IDLE menus but didn't see anything. Is there a tool that can determine all the...
59
by: kk_oop | last post by:
Hi. I wanted to use exceptions to handle error conditions in my code. I think doing that is useful, as it helps to separate "go" paths from error paths. However, a coding guideline has been...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
5
by: Paul Sijben | last post by:
All, in a worker thread setup that communicates via queues is it possible to catch exceptions raised by the worker executed, put them in an object and send them over the queue to another thread...
10
by: John Nagle | last post by:
Here are three network-related exceptions. These were caught by "except" with no exception type, because none of the more specific exceptions matched. This is what a traceback produced: 1....
0
debasisdas
by: debasisdas | last post by:
In PL/SQL, a warning or error condition is called an exception. Exceptions can be internally defined (by the runtime system) or user defined. A runtime error such as stack overflow or division...
14
by: Rafe | last post by:
Hi, I've encountered a problem which is making debugging less obvious than it should be. The @property decorator doesn't always raise exceptions. It seems like it is bound to the class but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
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...

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.