Exception when using AS in C #

When recording

var e = element.NextSibling as HtmlElement;

I get an invalid exception. I thought that prevent cast exception and return null? because that is what I wanted / except. Or tell me, how can I get the value of e instead of an exception? or why did I get this exception when using the as keyword?

-edit- Fix this was an element. NextSibling throws an exception. How strange that his throwing exceptions cast when I havent tried it yet. I'm not sure how to check if the next brother exists. Error in NextSibling below

    Message "Unable to cast COM object of type 'System.__ComObject' to interface type 'IHTMLElement'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F1FF-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."  string
+3
source share
1 answer

Are you sure this is a specific statement that throws an exception?

try/catch, , , :

var next = element.NextSibling;
var e = next as HtmlElement;

, .NextSibling?

as #, , , . , , , .

. @Jon Skeet , , , , . , , .

, , HTML-. element HtmlElement, .NextSibling , , .NextSibling , , , .

. , .

, .NextSibling COM-, , , IHTMLElement, . , , , , , .

, , .NextSibling try/catch, , - HTML, , .

.

+13

Source: https://habr.com/ru/post/1713387/


All Articles