Try Catch block not catching?

netsmithcentral
netsmithcentral used Ask the Experts™
on
I have a simple function which tries to get a value from an Obout grid filter column, and if the value is empty, ignores it and moves on.  For some reason this code ignore my catch block and always shows a System.FormatException when the input string is empty!

More bizarre, if I use visual studio's debugger and set a breakpoint on that line, the catch block functions normally (after I continue from that line).  I have already confirmed that my Debug | Exceptions | CLR are not set to catch when thrown.  I have also confirmed this same behavior in the production version.

    'Get the month selected
        Dim MonthSelected As Integer
        Try
            MonthSelected = CInt(DateCreatedColumn.FilterCriteria.Value)
        Catch ex As Exception
            'If value is empty / not a number reset the filter
            DateCreatedColumn.FilterCriteria.FilterExpression = String.Empty
            Return
        End Try
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
I would check the value of the DateCreatedColumn.FilterCriteria.Value

use something like this:
This way if it has trouble parsing num will be -1
Dim num As Integer
If Not Integer.TryParse(DateCreatedColumn.FilterCriteria.Value, num) Then
	    num = -1
End If

Open in new window

kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Which method of the Page class is this code in?
Top Expert 2015
Commented:
Using Try...Catch to define the flow of a program is never a good situation. Since you know that this situation can cause a problem, it would be a lot better to use If...End If to detect the situation and correct it. This would make the application a little faster and would not used up resources (a Try...Catch must build the Exception object).

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial