Exception handling in web user control

E

Efi Merdler

Hi,
I created a user control, but instead of handling exception in the
user control level I prefer to handle them in the containing form.

In the load event of the containing form I'm using:
entryWindow.Error += new EventHandler(Page_Error);

when entryWindow is my user control.

Two questions:
1. Is this the correct way of doing that ?
2. When exception occurs I'm setting some of my controls' visibility
property to false, however it seems that asp.net ignores it and still
displays them, why ?

Thank you,
Efi
 
J

John Saunders [MVP]

Efi Merdler said:
Hi,
I created a user control, but instead of handling exception in the
user control level I prefer to handle them in the containing form.

In the load event of the containing form I'm using:
entryWindow.Error += new EventHandler(Page_Error);

when entryWindow is my user control.

Two questions:
1. Is this the correct way of doing that ?
2. When exception occurs I'm setting some of my controls' visibility
property to false, however it seems that asp.net ignores it and still
displays them, why ?

It looks like you are violating encapsulation. You are giving the containing
control too much knowledge of the internals of the user control. You're
asking the container to understand the exceptions thrown by the user
control. You might get away with that right now, when there's only one
possible containing control, but you should ask yourself what would happen
if you needed to use that user control in another container. Wouldn't the
other container also need that same knowledge of the user control?

Instead, why not let the user control handle its own exceptions and do with
them what needs to be done. For instance, if there's a place in the user
control where some controls are databound, and there's an exception thrown
while accessing the database, then the user control itself should perhaps
disable the controls which would have been data bound. That's not something
that a containing page should have to know about.

Personally, I never use the Error event of a page. Instead, I use try/catch
blocks, and only catch exceptions if I'm going to do something intelligent
with them. That could be something as simple as logging the exception and
displaying an error label; to adding information to the exception and
rethrowing. In rare cases, one might catch an exception that means that the
user entered data which has led to an "expected error" result. An example
might be the user entering data which would result in a database constraint
violation. In that case, I would catch the exception and turn it into a
purely UI event, possibly displaying an error message.

If the containing control is calling a method or property of the user
control, and that method or property throws an unhandled exception, _then_
the containing control should handle the exception:

string lookupKey = Request.QueryString["LookupKey"];
try
{
myUserControl.LookupKey = lookupKey;
myUserControl.DataBind();
}
catch (DataException dEx) // Note that if it threw NullReferenceException,
for instance, then global error handling would deal with it
{
lblError.Text = "No such key " + lookupKey;
lblError.Visible = true;
}

In any case, I suggest you keep exception handling as local as possible, and
limit it to only those exceptions you know what to do about.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top