Sunday, 15 May 2011

c# - InvalidOperationException not being caught in lambda expression -



c# - InvalidOperationException not being caught in lambda expression -

i catching invalidoperationexception around utilize of xmlserializer.deserialize. unusual thing when provide malformed xml indeed throw invalidoperationexception on line not caught grab block.

my first assumption is unusual exception handling in lambdas unable find conclusive upon searching.

anyone have thoughts?

also, please note, code work non malformed xml. also, invalidoperationexception in same namespace of system

xelement xmlelement = getelement("users"); xmlserializer deserializer = new xmlserializer(typeof(user)); seek { homecoming xmlelement.descendants("user").select(x => (user)deserializer.deserialize(x.createreader())); } grab (invalidoperationexception e) { //xmlserializer throws ambiguous exception inner exception much more explicit //creating new exception message of old ensure there isn't circular throw-catch of invalidoperationexception throw new xmlexception(e.innerexception.message); }

i don't know how homecoming value used, select lazy executes lambda when result collection enumerated. think that's why catch not work - @ time when exception thrown, it's out of catch block.

adding toarray end should forcefulness execute immediately:

return xmlelement.descendants("user").select(x => (user)deserializer.deserialize(x.createreader())).toarray();

quote msdn:

this method implemented using deferred execution. immediate homecoming value object stores info required perform action. query represented method not executed until object enumerated either calling getenumerator method straight or using foreach in visual c# or each in visual basic.

c# exception lambda

No comments:

Post a Comment