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