0

I have the following code block and I wonder why a run time error is thrown rather than me being able to handle the exception within catch (I put a breakpoint at int i = 1; but it is never reached):

var stringArray = textRow.Split(Delimiter);

try
{
    var a = DateTime.Parse(stringArray[0]);
    var b = double.Parse(stringArray[2]);
    var c = double.Parse(stringArray[3]);
}
catch (Exception e)
{
    int i = 1;

}

The following FormatException Occurred error is thrown:

System.FormatException occurred
  _HResult=-2146233033
  _message=The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.
  HResult=-2146233033
  IsTransient=false
  Message=The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.
  Source=mscorlib
  StackTrace:
       at System.DateTime.Parse(String s)
  InnerException: 

I understand that I can use TryParse but I like to test whether try/catch will make for better performance because I iterate over this code block many millions of times.

Thanks

awesoon
  • 32,469
  • 11
  • 74
  • 99
Matt
  • 7,004
  • 11
  • 71
  • 117
  • purely out of curiosity, what is a sample contents of textRow so we could simply recreate this code as a small program? :) – Icepickle Jul 28 '15 at 05:30
  • Can you provide the sample data? – Carbine Jul 28 '15 at 05:30
  • Does it work when you catch `FormatException` explicitly? – Nikolai Samteladze Jul 28 '15 at 05:31
  • A sample value is: "2014.11.02 22:00:01.996,1.25099,1.25067,1.5,0.37" – Matt Jul 28 '15 at 05:35
  • @NikolaiSamteladze, no it does not get caught either. – Matt Jul 28 '15 at 05:35
  • im just guessing here, but it DateTime is a struct and mscorlib is unmanaged and since .Net 3 unmanaged exceptions need to be catched explicitly – Sebastian L Jul 28 '15 at 05:43
  • The "_HResult" and "_message" looks like an internal exception... can you provide a clip image of your IDE with the exception popup window? – J.C Jul 28 '15 at 05:44
  • 1
    a side note: dont use try/catch against Tryparse. for the performance difference take a look at [here](http://stackoverflow.com/questions/150114/parsing-performance-if-tryparse-try-catch) – M.kazem Akhgary Jul 28 '15 at 05:45
  • @J.C, I ticked "Thrown" within Debug/Exceptions/CLR Exceptions. Is that the issue potentially? – Matt Jul 28 '15 at 05:46
  • @M.kazemAkhgary, interesting, I guess that is what I was looking for. I just wondered why the error is still thrown instead of caught. – Matt Jul 28 '15 at 05:47
  • If you used this code, breakpoint should be hit. Check you premise...Probably your breakpoints are not activated? Are you running in debug mode? Try to restart VS?...Does any other breakpoint get hit when you run the application? – wonderbell Jul 28 '15 at 05:51
  • @aSharma, tried that already and yes, other breakpoints get hit. – Matt Jul 28 '15 at 05:53
  • @MattWolf, I think this is a problem like an internal exception of unmanaged code. Perhaps you should try Fratyx's answer. – J.C Jul 28 '15 at 05:56

1 Answers1

2

Try continuing debugging after the exception occurred. The message says that a first chance exception occured but you can continue the program. You should reach the breakpoint then. If there were no try.. catch block you would get an UNHANDLED exception.

Fratyx
  • 5,717
  • 1
  • 12
  • 22
  • you are accurate, is the first chance exception thrown because I am within the debugging environment? It would not occur in the release but simply caught for me to decide how to handle it and potentially re-throw? – Matt Jul 28 '15 at 05:49
  • Yes. You can change this behavior in Visual Studio in menu Debug->Exceptions. There you can decide for which Exceptions you are notified if they are thrown while debugging – Fratyx Jul 28 '15 at 05:57