From: Bob on 20 May 2010 09:35 I've run into an awkward exception. I'm using interop to a microsoft function that gets a DateTime from the web. Occasionally one will come in as a null (shows as 12/30/1899 12:00:00 am), and generates an exception: Not a legal OleAut date. I'm copying the incoming DateTime to a "DateTime?", so the problem seems to come when I access the source variable (wrapper for the MS Com object). I've written a function that copies the incoming DateTime inside a try/catch. I've also tried the same with an extension method. Nothing seems to catch it. Another odd thing: Though the exception gets tripped inside VS2008, with the familiar yellow popup, execution can continue from that point. Has anyone seen this before? How did you get around it?
From: Bob on 20 May 2010 23:40 On Thu, 20 May 2010 09:35:22 -0400, Bob <bob(a)nope.com> wrote: >I've run into an awkward exception. I'm using interop to a microsoft >function that gets a DateTime from the web. Occasionally one will come >in as a null (shows as 12/30/1899 12:00:00 am), and generates an >exception: Not a legal OleAut date. > >I'm copying the incoming DateTime to a "DateTime?", so the problem >seems to come when I access the source variable (wrapper for the MS >Com object). > >I've written a function that copies the incoming DateTime inside a >try/catch. I've also tried the same with an extension method. Nothing >seems to catch it. > >Another odd thing: Though the exception gets tripped inside VS2008, >with the familiar yellow popup, execution can continue from that >point. > >Has anyone seen this before? How did you get around it? OK, this is officially weird. The previous code was generating an exception on the first line below: rssItem.Modified = item.Modified; rssItem.PubDate = item.PubDate; rssItem.LastDownloadTime = item.LastDownloadTime; ... The variables at the left are in my own code. All are declared as "DateTime?" (nullable DateTime) because some of the incoming DateTime fields are null (they show up as "12/30/1899 12:00:00 am", which is all zeros). The items at the right side are straight DateTime objects coming from Com interop to a Microsoft DLL. The declaration of the left-side items as "DateTime?" initially seemed to cure null exceptions. However, I've just started encountering exceptions again for some reason, presumably from accessing the fields at the right (you'd think Microsoft would have handled that!?). This time I wrote a dedicated function for transferring DateTime fields: public static DateTime? GetNullableDateTime(DateTime dt) { DateTime? nullableDT = null; try { nullableDT = dt; } catch (Exception ex) { nullableDT = null; } return nullableDT; } I figured that any access to the internal MS functions would generate an exception inside the "try" block. But the runtime exception still occurs! The second bizarre thing is that VS2008 pops up an exception window (info quoted above) but does not jump out of the loop where those are set. Execution continues with the next statement. (Not the case when running outside VS2008, of course) The third bizarre thing: It does not occur again. The 2nd and 3rd assignments above work fine. It seems to just occur once every time that the program is run. I can fix this by putting ugly try-catches around every single DateTime assignment (there are a bunch, scattered through several classes). But of course I'd love to resolve that in more elegant fashion. Has anyone seen anything like this?
|
Pages: 1 Prev: Solution using Expression tree Next: ((System.IO.Stream)(s)).ReadTimeout. What might be wrong? |