0

I'm testing my Visual Basic .net application by running it in Visual Studio express.

Sometimes it runs OK, but other times I get an exception (details below, if useful). I'm not sure why I get the exception as the particular part of the application that I am testing re-uses code that works OK for other features.

What should I check, what would give me a hint as to the root cause.

Things I tried so far are:

  • Rewriting some of the code to be more robust. The outcome of this was that this approach was getting out of control - I was correcting everything. I made changes such asing alternative libraries (e.g. replacing CInt with convertTo etc). Some lazy declarations, but it occurred to me that there was no problem with these with the code before my changes. And every change I seemed to solve uncovered yet another problem, another different exception
  • So I thought something must be fundamentally wrong with my installation, and a search found discussion group posts from people experiencing something similar. The suggested remedy would be to re-install the .net framework. So I did that and the problems still occured.

Any thoughts on approach to get to the root of the problem? I'm not asking for a solution but some fresh ideas would be very welcome.

Update:

I've added in the following code to get more detail (+1 thanks @Meta-Knight)

        Dim exceptionMessage As String = ex.Message
        Console.WriteLine("Message: \n" & exceptionMessage)
        Dim exceptionTargetSite As String = ex.TargetSite.ToString
        Console.WriteLine("Inner: \n" & ex.TargetSite.ToString)
        Dim exceptionSource As String = ex.Source
        Console.WriteLine("Source\n:" & exceptionSource)
        ' put the stack trace into a variable
        ' (this helps debugging - put a break point after this is assigned
        ' to see the contents)
        Dim stackTrace As String = ex.StackTrace.ToString()
        Console.WriteLine("Stack trace\n:" & stackTrace)

More details about exception

Error - Exception Message "Arithmetic operation resulted in an overflow."

Exception Target Site: "Int32 ToInteger(System.String)"

Exception Source: "Microsoft.VisualBasic"

at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) at MyCo_3rd-Party-Industrial-Printing-System.Customer_EanUpc_serialNumberGeneratorAssembly.btnPrint_Click(Object sender, EventArgs e) in C:\labelprint\MyCo 3rd-Party-Industrial-Printing-System v2\MyCo 3rd-Party-Industrial-Printing-System\PrintForms\Customer_EanUpc_serialNumberGeneratorAssembly.vb:line 271 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.PerformClick() at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.PreProcessMessage(Message& msg) at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg) at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg) at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.RunDialog(Form form) at System.Windows.Forms.Form.ShowDialog(IWin32Window owner) at System.Windows.Forms.Form.ShowDialog() at MyCo_3rd-Party-Industrial-Printing-System.utlForm.openNewModalForm(String form) in C:\labelprint\MyCo 3rd-Party-Industrial-Printing-System v2\MyCo 3rd-Party-Industrial-Printing-System\Utilities\utlForm.vb:line 128

Update 2:

The code around line 272 was:

        For i As Integer = 1 To CInt(numQuantity)
            If generationMethods() Then

The code around line 272 is now (I've adjusted it):

        Dim numQuantityAsStr As String = numQuantity.Text
        Dim numQuantityAsInt As Integer = Convert.ToInt32(numQuantityAsStr)
        For i As Integer = 1 To numQuantityAsInt
            If generationMethods() Then

numQuantity is a String variable for a field in the Windows form I am using and this has a value put in it by the user, the field is used to specify the quantity of something so this variable is converted to a integer so that it can be used in a for loop. The test value I am using is always entering 1 in this field, and sometimes this causes the exception.

My alteration uses what I think is a more modern type conversion Convert.ToInt32 rather than CInt I've also introduced intermediate steps to help debug.

The exception now does not occur here but I did try this the other week and another separate exception popped up (if I remember) from somewhere else so we'll see if this fixes it. Any thoughts as to why using a different conversion utility would solve the problem?

Update 3: An Exception now occurs elsewhere: (But why?! This one is thrown from library code!)

The following code caused it:

Dim dateNowAsStr As String = DateTime.Now.Date.ToString

Exception Message: "Value to add was out of range. Parameter name: value"

Exception target site: "System.DateTime Add(Double, Int32)"

Exception source: "mscorlib"

" at System.DateTime.Add(Double value, Int32 scale) at System.TimeZoneInfo.TransitionTimeToDateTime(Int32 year, TransitionTime transitionTime) at System.TimeZoneInfo.GetDaylightTime(Int32 year, AdjustmentRule rule) at System.TimeZoneInfo.GetIsDaylightSavingsFromUtc(DateTime time, Int32 Year, TimeSpan utc, AdjustmentRule rule, Boolean& isAmbiguousLocalDst) at System.TimeZoneInfo.GetDateTimeNowUtcOffsetFromUtc(DateTime time, Boolean& isAmbiguousLocalDst) at System.DateTime.get_Now() at GenerationLibrary.GenerationUtilities.callserialNumberGenerator(String serialNumberGeneratorSnFile, String serialNumberGeneratorRange, DefaultSettingsHandler settings) in C:\labelprint\MyCo 3rd-Party-Industrial-Printing-System v2\GenerationLibrary\GenerationUtilities.vb:line 28 at GenerationLibrary.MyCoSnGeneration.constructMyCoSn(DataRow& oDataRow, DefaultSettingsHandler& settings) in C:\labelprint\MyCo 3rd-Party-Industrial-Printing-System v2\GenerationLibrary\MyCoSnGeneration.vb:line 18 at MyCo_3rd-Party-Industrial-Printing-System.Customer_EanUpc_serialNumberGeneratorAssembly.generationMethods() in C:\labelprint\MyCo 3rd-Party-Industrial-Printing-System v2\MyCo 3rd-Party-Industrial-Printing-System\PrintForms\Customer_EanUpc_serialNumberGeneratorAssembly.vb:line 40 at MyCo_3rd-Party-Industrial-Printing-System.Customer_EanUpc_serialNumberGeneratorAssembly.btnPrint_Click(Object sender, EventArgs e) in C:\labelprint\MyCo 3rd-Party-Industrial-Printing-System v2\MyCo 3rd-Party-Industrial-Printing-System\PrintForms\Customer_EanUpc_serialNumberGeneratorAssembly.vb:line 275 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.PerformClick() at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.PreProcessMessage(Message& msg) at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg) at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg) at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.RunDialog(Form form) at System.Windows.Forms.Form.ShowDialog(IWin32Window owner) at System.Windows.Forms.Form.ShowDialog() at MyCo_3rd-Party-Industrial-Printing-System.utlForm.openNewModalForm(String form) in C:\labelprint\MyCo 3rd-Party-Industrial-Printing-System v2\MyCo 3rd-Party-Industrial-Printing-System\Utilities\utlForm.vb:line 128"

Update 4

BUT

Dim dateNowAsStr As String = CStr(DateTime.Now)

does not cause the exception

therobyouknow
  • 6,604
  • 13
  • 56
  • 73
  • 1
    You showed the Stack Trace but not the actual error... – Meta-Knight Apr 12 '12 at 13:24
  • 1
    [Do you have Option Strict On?](http://www.getdotnetcode.com/gdncstore/free/Articles/OPTION%20STRICT%20ON.htm) If not you need to get this in your life. – Matt Wilko Apr 12 '12 at 13:44
  • @Matt +1 I will check if this is on. Thanks for the link. – therobyouknow Apr 12 '12 at 13:56
  • Option Strict On is now on. It was off. I have Rebuilt the solution. No errors seen. However, thanks for this as I agree it was necessary to help eliminate some things. and generally best practice I would agree. – therobyouknow Apr 12 '12 at 14:17
  • I have a suspicion that the root cause lies in the way threads are being used. I will investigate further thanks for your input. – therobyouknow May 11 '12 at 14:30

2 Answers2

1

I think you can start by looking at what is at line 271 of the Customer_EanUpc_serialNumberGeneratorAssembly.vb source code file. Which is located: C:\labelprint\MyCo 3rd-Party-Industrial-Printing-System v2\MyCo 3rd-Party-Industrial-Printing-System\PrintForms\ directory.

It looks like the problem is related to a conversion between string to integer where maybe it failed because the string cannot be converted to integer... maybe it has alphanumeric characters... "Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)"

Did you already check that?

Why you get the error some times and not always could be because (and I'm guessing here) the code in serialNumberGeneratorAssembly sometimes generates numeric only values (that can be correctly converted to integer) and some other times generates alphanumeric serial numbers (that throw the convertion exception).

UPDATE: the code that generates the Serial Numbers is generating numbers bigger than an Integer value. Try converting to Long instead of Integer... or have a look at System.Numerics.BigInteger in case those numbers are too big. That explains the: "Arithmetic operation resulted in an overflow." exception message.

Carlos Quintanilla
  • 12,937
  • 3
  • 22
  • 25
  • +1 @Carlos Quintanilla thanks for your suggestion, I did try to make the conversion code more robust, but after that a different exception occurred elsewhere. So it led me to believe that something more serious was occurring with my system... – therobyouknow Apr 12 '12 at 13:34
  • 1
    It's worth noting that vb.net has some really obnoxious rules about numeric types, some of which borrow from the .net, framework and some of which are unique to vb.net. The statement `IntVar = ByteVar1 + ByteVar2` will throw an exception if the sum exceeds 255. This behavior (regarding the sum as a `Byte`) was probably added to avoid the need for an explicit typecast when doing something like `ByteVar1 = ByteVar2 + ByteVar3` (in C#, a narrowing typecast from `int` to `unsigned char` would be required) but it's still silly and annoying. – supercat Apr 12 '12 at 15:10
  • +1 @supercat point worth knowing. But I have a suspicion that the root cause lies in the way threads are being used. I will investigate further thanks for your input. – therobyouknow May 11 '12 at 14:31
  • 1
    @therobyouknow: The VB handling of byte+byte arithmetic has certainly bitten me in the past and is, to put it simply, mind-boggling. Why VB can't have a concept of "expression of type 'int' which has roots in 'byte'" is beyond me. Nonetheless, since vb.net is what it is, it's important to be aware of its behavior. Incidentally, I'm also curious why the type of "unsignedThing1 And unsignedThing2" is the wider of the two types, since the result can never exceed the domain of the smaller. – supercat May 11 '12 at 15:15
1

Before rewriting or reinstalling anything, you should try to identify the source and the reason for the error.

The first thing to do is to analyse the error message and stack trace. If you include debugging files (.pdb) with your application's files, you will get more detailed information such as the line number which can be helpful.

If that doesn't give enough information about the circumstances of the error, then you can add some logging to your app. By logging what the user does in your app it might help reproduce the problem.

Finally you can always get help by searching on google or asking on StackOverflow, but make sure to include the actual error message, not just the stack trace... You should also post the code where the error happens.

Edit:

So the actual error is: "Arithmetic operation resulted in an overflow."

Googling this would have helped: you would have found out that such an error happens when you're trying to convert a number which is out of bounds for an integer. If you expect to have some very large numbers you can try converting to Long instead.

Meta-Knight
  • 17,626
  • 1
  • 48
  • 58
  • 1
    Not necessarily out of bounds for an integer. In vb.net, the sum of two `Byte` values is also a `Byte`. – supercat Apr 12 '12 at 15:11
  • 1
    @supercat: In this case the error happens when a string value is converted to an Integer. In such a case the only possibility is that the number is too small or too large to fit into an Int32. – Meta-Knight Apr 12 '12 at 15:45
  • +1 supercat, +1 Meta-Knight - points worth knowing. But I have a suspicion that the root cause lies in the way threads are being used. I will investigate further thanks for your input. – therobyouknow May 11 '12 at 14:31