Results 1 to 13 of 13

Thread: [RESOLVED] I don't understand this Exception Text

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Resolved [RESOLVED] I don't understand this Exception Text

    Hi,
    I don't understand why I occasionally get an exception whilst running an app. in my machine.

    This app. runs every time the machine starts or wakes-up and usually continues until the HDD activity drops below a (quite low) level and remains low for more than 20 seconds, at which time the app. closes.

    Occasionally an un-trapped error occurs giving a large details list.
    I don't understand what it's telling me, it appears to say that the error occurs in several methods at the same time:
    ************** Exception Text **************
    System.OverflowException: Arithmetic operation resulted in an overflow.
    at ActivityZero.Form1.Times(Int32 sin)
    at ActivityZero.Form1.Record()
    at ActivityZero.Form1.Timer1_Tick()
    at ActivityZero.Form1._Lambda$__R27-2(Object a0, EventArgs a1)
    at System.Windows.Forms.Timer.OnTick(EventArgs e)
    at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    *****************************************
    Times(Int32 sin), Form1.Record() and Timer1_Tick() are three of my methods, I don't recognise the others.
    I don't understand how to read this exception and therefore don't know where to start looking for the problem.
    Especially as it only occurs once in a while.


    Poppa.
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,927

    Re: I don't understand this Exception Text

    The "at ..." section is the Stack Trace, and the one at the top is the one where the error occurred.

    The ones below it are the things that caused the routine to run (the ones you don't recognise are parts of the .Net framework), so in this case, reading from the bottom: Timer1_Tick was run (due to the framework triggering it), and it called Record, and that called Times - which is where the error occurred.


    It may seem like superfluous information, but a routine can be called from multiple places, and the parameters passed in (or other data that the caller set) can be the thing that triggered the exception, so knowing the chain of routines that led there can sometimes be very useful.

    In general however you only really need to know about the item at the top (in this case Times), as that is where the exception happens.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: I don't understand this Exception Text

    The exception is an arithmetic overflow so you are almost certainly adding two Integer values together in that Times method and the result is usually within the range of the Integer type but sometimes too large. For instance, this loop will run fine for a long time but eventually throw that exception:
    vb.net Code:
    1. Dim n = 0
    2.  
    3. Do
    4.     n += 1
    5. Loop
    Once n gets to 2,147,483,647, adding 1 to it will generate a value beyond the range that an Integer can store.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: I don't understand this Exception Text

    Since you're passing a variable in, if the variable happens to be 0 for some reason, and you're using it to divide, that can also generate an overflow operation.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: I don't understand this Exception Text

    Quote Originally Posted by passel View Post
    Since you're passing a variable in, if the variable happens to be 0 for some reason, and you're using it to divide, that can also generate an overflow operation.
    Wouldn't that cause a DivideByZeroException rather than an OverflowException? Division of a Double or Single by zero yields NaN but other numeric types produce DivideByZeroExceptions.

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: I don't understand this Exception Text

    Depends. Since the value is an Integer, and if you're doing a divide the result will be a float type, you may need to convert it like:
    CInt(val / sin)

    I assume because it has to pass the result to the CInt function, that NaN causes an OverflowException.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: I don't understand this Exception Text

    Quote Originally Posted by passel View Post
    I assume because it has to pass the result to the CInt function, that NaN causes an OverflowException.
    Right, the documentation for OverflowException does indicate that it can result from a cast, which would presumably be when casting a value that is too large for the target type. The example you show could fit that description, so it would be the cast, rather than the division, that was responsible for the exception.

  8. #8

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: I don't understand this Exception Text

    I can't discover why I'm not getting emails when I have a reply !

    Poppa.
    Along with the sunshine there has to be a little rain sometime.

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: I don't understand this Exception Text

    Quote Originally Posted by jmcilhinney View Post
    Right, the documentation for OverflowException does indicate that it can result from a cast, which would presumably be when casting a value that is too large for the target type. The example you show could fit that description, so it would be the cast, rather than the division, that was responsible for the exception.
    Right. The cast causes the exception. If you didn't cast, there wouldn't be an exception at the point of the division, as VB.Net doesn't raise an exception for the divide by 0, it returns a NaN result and that can cause an exception later when you try to use it.

    Perhaps I should have said a divide by 0 can lead to an overflow or other exception later.
    In any case, I have Option Strict On, so the explicit cast was necessary.

    Quite a few users don't have that on, in which case the following code, because of the unseen implicit cast, raises the same Overflow exception, so it looks like dividing by 0 causes the exception, where technically it doesn't, but it does lead to the exception.
    Code:
        Dim a As Integer
        '   Shell("cmd.exe /k echo Hello", AppWinStyle.Hide)
        Dim i As Integer
        i = 5 / a
    And the exception popup when running in the IDE, does mention making sure you're not dividing by 0.
    Name:  DivideByZero.png
Views: 727
Size:  19.9 KB
    Last edited by passel; Jun 8th, 2019 at 08:37 PM.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: I don't understand this Exception Text

    Quote Originally Posted by passel View Post
    Right. The cast causes the exception. If you didn't cast, there wouldn't be an exception at the point of the division, as VB.Net doesn't raise an exception for the divide by 0, it returns a NaN result and that can cause an exception later when you try to use it.

    Perhaps I should have said a divide by 0 can lead to an overflow or other exception later.
    In any case, I have Option Strict On, so the explicit cast was necessary.

    Quite a few users don't have that on, in which case the following code, because of the unseen implicit cast, raises the same Overflow exception, so it looks like dividing by 0 causes the exception, where technically it doesn't, but it does lead to the exception.
    Code:
        Dim a As Integer
        '   Shell("cmd.exe /k echo Hello", AppWinStyle.Hide)
        Dim i As Integer
        i = 5 / a
    And the exception popup when running in the IDE, does mention making sure you're not dividing by 0.
    Name:  DivideByZero.png
Views: 727
Size:  19.9 KB
    It's all so clear to me now. It's also worth noting that VB has an integer division operator, which many people don't know. Using it is like performing regular division and then truncating the result, which may or may not be what people want when dividing one Integer by another to get an Integer. They may prefer rounding to truncation.

  11. #11

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: I don't understand this Exception Text

    Thanks guys, Now I know where to look, I may've found the culprit.
    Time will tell...

    I'm still not getting VB forum emails !


    Poppa.
    Along with the sunshine there has to be a little rain sometime.

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,927

    Re: I don't understand this Exception Text

    Good stuff, hopefully you've got it sorted.

    Quote Originally Posted by Poppa Mintin View Post
    I'm still not getting VB forum emails !
    Some email providers are occasionally a bit over-zealous when it comes to detecting spam, and yours might be treating the VBForums emails as spam. If you login to the webmail, you can check if mails from here have gone in to the spam folder, and if so do something about it there (which may be "mark as not spam", but it varies a lot between email providers).

  13. #13

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: I don't understand this Exception Text

    Thanks for the suggestion Si_the_geek, but as it happens, I see all my emails before they're segregated so I know that's not the reason.
    Until recently the emails arrived correctly and I don't believe I've done anything to change that.
    I've re-checked that my email address is correct in the profile, and changed the password just in case that's relevant.


    Poppa.
    Along with the sunshine there has to be a little rain sometime.

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