tags:

views:

125

answers:

2

Ok, so after spending a good portion of a day debugging a stupid typing mistake inside a piece of code I am curious as to why the specific actions occured rather than an exception.

First of all the problem code.

Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
    Dim hl As New HyperLink
    AddHandler hl.DataBinding, AddressOf Me.BindData
    container.Controls.Add(container)
End Sub

The obvious problem is that we are trying to add the container to itself, which I would have expected to cause an exception. However, instead it caused the page to prompt the user for their login credentials (Windows authentication in the browser).

Does anyone have an idea why this is the case, and why an exception or something else didn't happen?

EDIT

The reason for the question is that due to this mistake, the page is rendered useless, and prompts for Windows Login, and NOT giving stack overflow exceptions or any other exception.

+1  A: 

Where would you expect the exception to happen? Inside this code there is no reason an exception should be generated. However illogical it may be to nest a control within a control it is still valid. It won't paint very well and unless the painting/drawing layer is specifically aware of nesting then that could cause a Stack Overflow/Infinite loop. But that wouldn't occur here, but during layout/paint.

JaredPar
Added clarification to the question, it is possible that it was during layout/paint, but the end result is that this code didn't generate an exception, but prompted the user for windows authentication.
Mitchel Sellers
+1  A: 

Controls are something that can be arbitrarily nested; anything that is a Control can have a collection of other controls (I'm sure the reason for this is obvious). That being said, there's nothing "wrong" with adding a reference of a given object to a collection of such objects within itself. Though redundant, it's not exceptional.

J Cooper
Added clarification to the question, it is possible that it was during layout/paint, but the end result is that this code didn't generate an exception, but prompted the user for windows authentication.
Mitchel Sellers