4

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
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
just.another.programmer
  • 8,579
  • 8
  • 51
  • 90
  • What happens if you continue running *after* it hits the breakpoint in the debugger? – 3Dave Aug 08 '11 at 13:52
  • Try deleting your temp files, asp.net temp files, obj folders, rebuild and retry. Even restart IDE. It looks like the debug symbols being loaded by default are out of sync. – Mrchief Aug 08 '11 at 13:54
  • Why do you have a `Return` in your `Catch` block? – Jodrell Aug 08 '11 at 13:57
  • have you confirmed what line that exception is thrown on? There is defintiely no possibility that this exception is being thrown by what you are doign in the catch block? – Chris Aug 08 '11 at 13:57
  • @David Lively, program runs normally post breakpoint – just.another.programmer Aug 08 '11 at 14:00
  • @Mrchief, I've restarted, cleared files, republished, and everything else, no difference – just.another.programmer Aug 08 '11 at 14:02
  • @Jodrell, there's more code after the try catch block which I don't want to execute if there was an error – just.another.programmer Aug 08 '11 at 14:03
  • @Chris, I double checked the line numbers, they indicate it's the MonthSelected = CInt line. – just.another.programmer Aug 08 '11 at 14:03
  • @user794234: Hmmm... odd. I've no idea then. I've never seen any such behaviour. If you go into debug mode and pause at that line and run the CInt (eg just in the watch window) does it throw the exception? – Chris Aug 08 '11 at 14:09
  • @Chris I tried that, it throws the exception and automatically catches it in the debugger window (instead of outputting to the error page the web browser). The debugger immediate window must run on a different memory space than the program its deubbing. – just.another.programmer Aug 08 '11 at 14:11
  • that is very weird. If you put a `throw new Exception("This is a test")` does it catch that? Just thinking that it might at least confirm that it is just not catching rather than something odd about the integer parsing... – Chris Aug 08 '11 at 14:13

2 Answers2

2

I think the reason this is happening is because you can't cast a null value to an Int, so the cast fails before the catch has a chance to get the exception.

Beyond that, I think you need to rewrite this code. It's not a good idea to use an Exception as part of your flow control. Exceptions are computationally expensive and should only be used in exceptional cases. A case you can plan for and program around is, by definition, not exceptional. Use if statements to check for nulls and such, don't use exceptions.

Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
  • in particular in the if statement you can use int32.TryParse which will return a boolean determining whether your integer parsed successfully (and output parameter to give you the integer). – Chris Aug 08 '11 at 14:03
  • It's not a null, it's an empty string. And shouldn't I get an exception either way (either NullReference or FormatException)? Didn't realize about the price of try/catch block, i'll look into switching to an if/then statement. May make the whole question a moot point. – just.another.programmer Aug 08 '11 at 14:04
0

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

MGZero
  • 5,812
  • 5
  • 29
  • 46