-
Jun 7th, 2019, 04:34 PM
#1
Thread Starter
PowerPoster
[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.
-
Jun 7th, 2019, 04:52 PM
#2
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.
-
Jun 8th, 2019, 02:07 AM
#3
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:
Once n gets to 2,147,483,647, adding 1 to it will generate a value beyond the range that an Integer can store.
Last edited by jmcilhinney; Jun 8th, 2019 at 02:11 AM.
-
Jun 8th, 2019, 12:28 PM
#4
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.
-
Jun 8th, 2019, 12:45 PM
#5
Re: I don't understand this Exception Text
Originally Posted by passel
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.
-
Jun 8th, 2019, 01:20 PM
#6
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.
-
Jun 8th, 2019, 01:33 PM
#7
Re: I don't understand this Exception Text
Originally Posted by passel
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.
-
Jun 8th, 2019, 04:31 PM
#8
Thread Starter
PowerPoster
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.
-
Jun 8th, 2019, 08:27 PM
#9
Re: I don't understand this Exception Text
Originally Posted by jmcilhinney
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.
Last edited by passel; Jun 8th, 2019 at 08:37 PM.
-
Jun 8th, 2019, 09:23 PM
#10
Re: I don't understand this Exception Text
Originally Posted by passel
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.
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.
-
Jun 9th, 2019, 05:52 PM
#11
Thread Starter
PowerPoster
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.
-
Jun 10th, 2019, 04:40 AM
#12
Re: I don't understand this Exception Text
Good stuff, hopefully you've got it sorted.
Originally Posted by Poppa Mintin
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).
-
Jun 10th, 2019, 04:57 AM
#13
Thread Starter
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|