For Programmers: Free Programming Magazines  


Home > Archive > C# > March 2005 > exception in static accessor









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author exception in static accessor
Stephan L.

2005-03-11, 8:57 am

Hi,
I have a problem.
I declare an accessor to a static property:

public static ClientComponent[] Components
{
get { return (ClientComponent[]) LoadedComponents.ToArray() ; }
}


When the accessor is called it throws an InvalidCastException. Whyever
it does that... What I am wondering about is:

The thrown exception is not propagated upwards. I can see it in the
debugger, when I watch the field. To the "outside" application it
behaves just like if I had wrapped around a try and an empty catch
after.
But when I catch the exception inside the accessor and print the
stacktrace I see that the stacktrace is lacking everything but the
method itself, so is only ONE entry deep.
What is going on there? Why is it like that?

Regards,
Stephan
Bruce Wood

2005-03-14, 3:57 am

I don't know why your exception isn't propagating normally. It should.
The only thing I can think of is are you calling this from within a
Form? Some odd things happen when you get exceptions within dialog
boxes, that differ between Debug and Release mode.

In answer to your other question, though, is that you're getting the
exception in the first place because you need to say:

public static ClientComponent[] Components
{
get { return (ClientComponent[])
LoadedComponents.ToArray(typeof(ClientComponent)) ; }
}

Of course, this may still throw an exception if the objects in your
LoadedComponents collection are not ClientComponents, but your original
code will _definitely_ throw an exception, because ToArray() with no
arguments will return an object[], not a ClientComponent[], so the cast
on the entire arry fails.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com