Error-bubbling

J

Jesper Stocholm

It was my impression, that .Net supported Error-bubbling, that is, any
errors would - without error-handling - be transported to the upper
class. However, it doesn't seem to be so.

The case is:

I have an ASP.Net-page that loads some controls to render content to
the page. Some of these controls retrieve data from a database, and
is therefore prone to any db-errors.

I would like to show the error on the page in a specific placeholder
(literal) så that it doesn't mess up the layout. I could implement
error-handling in the ascx-file itself, but then I cannot place the
error message correctly on the page, since the control doesn't have
access to the data on the page.

So I wrote the following code for my ASP.Net page:

private void Page_Load(object sender, System.EventArgs e)
{
LoadControls();
}

private void LoadControls()
{
try
{
divLeft.Controls.Add(Page.LoadControl("~/controls/search.ascx"));
}
catch (Exception e)
{
this.ErrorDescription = e.Message;
}
}

this.ErrorDescription is a member of the template page class my
pages all inherit from.

I thought that this would catch any problems ocurring in the
controls, but it doesn't. If I induce any errors in the controls,
the entire page crashes.

Am I missing something, or what should I do to fix it?

Thanks,
 
N

Nick Malik

I'm guessing that you caught errors on the page load, but not the other
events.

suggestion: put error handling in the events that trigger the db calls.

HTH,
--- Nick
 
J

Joe Mayo

Jesper Stocholm said:
It was my impression, that .Net supported Error-bubbling, that is, any
errors would - without error-handling - be transported to the upper
class. However, it doesn't seem to be so.

Just a clarification on terminology - it's called "exception handling". A
component throws an exception and it is up to the developer to determine
whether that exception truly is an error or not.
The case is:

I have an ASP.Net-page that loads some controls to render content to
the page. Some of these controls retrieve data from a database, and
is therefore prone to any db-errors.

I would like to show the error on the page in a specific placeholder
(literal) så that it doesn't mess up the layout. I could implement
error-handling in the ascx-file itself, but then I cannot place the
error message correctly on the page, since the control doesn't have
access to the data on the page.

If you have a Label control on your Web Form named "Label1" you can get to
it like this:

Label myLabel = (Label)Page.FindControl("Label1");
myLabel.Text = "This is where the message goes";

You can put this in the catch handler of the method in your control that
raises the exception.
So I wrote the following code for my ASP.Net page:

private void Page_Load(object sender, System.EventArgs e)
{
LoadControls();
}

private void LoadControls()
{
try
{
divLeft.Controls.Add(Page.LoadControl("~/controls/search.ascx"));
}
catch (Exception e)
{
this.ErrorDescription = e.Message;
}
}

this.ErrorDescription is a member of the template page class my
pages all inherit from.

I thought that this would catch any problems ocurring in the
controls, but it doesn't. If I induce any errors in the controls,
the entire page crashes.

Beyond try/catch, exception handling is totally inconsistent across all the
..NET technologies, whether it be Console, Windows Forms, ASP.NET, Web
Services, etc. So, since we're talking about exception handing in ASP.NET,
here are a couple other things to look at:

1. You can add an ErrorPage attribute to your @Page directive, which will
cause exceptions to be forwarded to a given page that you define with an
appropriate message.
2. ErrorPage isn't too much fun to maintain when you have hundreds of pages
on your site. So, you can also do error handling by implementing the
Application_Error method in Global.asax. This is my preferred method for
exceptions that I can't recover from.

Joe
 

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

Top