Results 1 to 10 of 10

Thread: [RESOLVED] System.DivideByZeroException ERROR (just on some computers)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Resolved [RESOLVED] System.DivideByZeroException ERROR (just on some computers)

    Hi,

    I made a downloader program which allows you to download files from the internet.
    I made the program using VB 2008 and a pc running windows 7 professional 32-bit. I also tested the program on a pc running windows vista home 32-bit, on another one running windows 7 pro 64bit, and on another one running Windows XP 32 bit. It works fine on all of them.

    But today I tried it for the first time on other three different PCs running:
    -Windows 7 starter (32-bit)
    -Windows 7 Home (64-bit)
    -Windows XP (32-bit)

    On all three of them it gave me an error right after I click the download button:
    "System.DivideByZeroException"

    this is the exact error (sorry it is in italian):
    Code:
    ************** Testo dell'eccezione **************
    System.DivideByZeroException: Tentativo di divisione per zero.
       in WindowsApplication1.Form1.Download_DownloadProgressChanged(Object sender, DownloadProgressChangedEventArgs e) in C:\Documents and Settings\Administrator\Desktop\downloader\Form1.vb:riga 221
       in System.Net.WebClient.OnDownloadProgressChanged(DownloadProgressChangedEventArgs e)
       in System.Net.WebClient.ReportDownloadProgressChanged(Object arg)
    If I click on "quit" the program closes, if I click on "continue" it just keeps working nice as it should do! (so i do not see the point of this error actually)
    (All the PCs have the latest framework 4.0 installed and anything else needed to make the program run correctly)

    Now, I figured out that the error happens here:
    Code:
    Dim kbps As Long = kbDownloaded \ Time
            If kbps < 1024 Then
                Label5.Text = String.Format("Download Speed: {0} KB/s", kbps)
            Else
                Label5.Text = String.Format("Download Speed: {0:0.00} MB/s", kbps / 1024)
            End If
    So here is what I did to "fix" it (note that I also had to change from backslash "\" to slash "/"):

    Code:
            If (kbDownloaded / Time) < 1024 Then
                Label5.Text = String.Format("Download Speed: {0} KB/s", kbDownloaded \ Time)
            Else
                Label5.Text = String.Format("Download Speed: {0:0.00} MB/s", (kbDownloaded / Time) / 1024)
            End If
    The thing is that right now it gives me a problem with the download speed! If I stop the download and then restart it (from the beginning) the download speed get crazy and it goes like 1kb/s, 2kb/s, 3kb/s, 4kb/s, 5kb/s, 6kb/s,.. and so on.

    How is all this even possible?
    The program works fine on four different PCs but then it gives that error on three others! Is there a way to fix this problem? I cannot believe that it just random works on some computers and on some other it does not.

    One more thing I would like to add is that the versions of windows where the program works are all in English language while the version of windows where the program does not work are all in Italian. Can this make the difference? If so, how can I fix it? I also tried to rebuild the exe on the one pc running windows xp in italian but it did not help! I also tried to rebuild it using visual studio 2010 but no luck

    Please help.

    Thank you in advance,

    Andrea
    Last edited by Netmaster; Feb 13th, 2011 at 01:18 AM.

  2. #2

    Re: System.DivideByZeroException ERROR (just on some computers)

    Change the slash back and perform a check to see if the time is equal to zero. If it is, don't run those. If it isn't, then you're good to go.

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: System.DivideByZeroException ERROR (just on some computers)

    This happens because "Time" is some very small number, say .4. When you divide using / there isn't a problem, but when you integer divide (\) time is being converted to an integer, and the conversion causes the number to be zero. So a couple of points:

    1 - Turn Option Strict On if it isn't.
    2 - Check that the divisor isn't zero before dividing!!!
    3 - Use normal division and then round accordingly.


    Code:
            Dim myTime As Double = 0.4
    
            Dim bytesDownloaded As Integer = 13
    
            Dim perSec, rps As Double
    
            If myTime <> 0 Then
                perSec = bytesDownloaded / myTime
    
                'different rounding choices
                Dim s As String = perSec.ToString("N0")
                Debug.WriteLine(s & " " & perSec.ToString)
    
                rps = Math.Round(perSec, 0, System.MidpointRounding.AwayFromZero)
                Debug.WriteLine(rps.ToString)
    
                rps = Math.Round(perSec, 0, System.MidpointRounding.ToEven)
                Debug.WriteLine(rps.ToString)
    
            Else
                'time zero ???
    
            End If
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: System.DivideByZeroException ERROR (just on some computers)

    wow. thanks for your help!

    i just did:
    Code:
    If Time > 0 Then
    'all the code in the download progress changed
    end if
    and it works! =O

    How is that possible? on some computers it works without the need of the "if" and on some other it does not...?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: System.DivideByZeroException ERROR (just on some computers)

    dbasnett sorry i just read your post.
    But how it is possible that on some computers it will work?
    do you think that if i leave the code as I did I will face some problem later? or not? Right now it is working fine:
    Code:
    If Time > 0 Then
    'all the code in the download progress changed
    end if
    Thank you,


    Andrea

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: System.DivideByZeroException ERROR (just on some computers)

    It could happen on any computer. The fact that it hasn't happened on them all (yet) doesn't mean it won't.

    Do the check for 0, then divide using / and use one of the methods to round the resulting value.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: System.DivideByZeroException ERROR (just on some computers)

    I noticed where you stated that it always happened on the Italian computers, but not on the English. That certainly suggests that there is some localization issue going on. What is Time in both systems? If you looked at the value on each system, it might look different, which could provide some insight as to what is happening.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: System.DivideByZeroException ERROR (just on some computers)

    basically noe i fixed it with by doing this:

    Code:
    If Time >= 1 Then
    'all the code in the download progress changed
    end if
    It works fine!

    Shaggy Hiker the old code actually worked on an italian pc as well (running windows vista 32-bit).
    The thing is that on 4 computers, the old code, works great ! ! ! on the other three computers it never works!
    I have tried several times already! I really do not know what to say!
    The thing is when the error pop out if I click on "Continue" the program works fine!

    Anyway, thanks a lot guys! =D

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: [RESOLVED] System.DivideByZeroException ERROR (just on some computers)

    I'd investigate a bit further, but only if you have the Dev environment on one of the computers that it doesn't work further on. Specifically, I would take a look at what was in Time when that condition failed. It may be that there is an obvious answer in those cases, or it could still be puzzling.
    My usual boring signature: Nothing

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] System.DivideByZeroException ERROR (just on some computers)

    Don't check for time > 0 and then do integer division (\)! Look at the code I provided.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width