Try catch block not catch?

I have a simple function that tries to get a value from an Obout grid filter column, and if the value is empty, ignores it and moves. For some reason, this code ignores my catch block and always throws a System.FormatException when the input string is empty!

More strange, if I use the visual studio debugger and set a breakpoint on this line, the catch block functions normally (after I continue with this line). I have already confirmed that my Debug | Exceptions | CLRs are not set to catch on a throw. I also confirmed the 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 
+4
source share
2 answers

I think the reason is that this is because you cannot pass the null value to Int, so the failure occurs before catch has the opportunity to get an exception.

Other than that, I think you need to rewrite this code. Using an exception as part of flow control is not recommended. Exceptions are expensive computing tools and should only be used in exceptional cases. A case that you can plan and program is, by definition, not exceptional. Use if to check for zeros, etc. Do not use exceptions.

+2
source

VB has something better for this, try the IsNumeric () method.

0
source

All Articles