Catching unhandled exceptions in web control to allow page to render
(I am actually using Ektron CMS, and am using Widgets
, which are a very simple extension of a user control).开发者_如何学Python
Problem: I have a web page that contains many widgets. If an exception occurs in one of these widgets, the page stops and the user is given a server error. I would like to have unhandled exceptions in a widget to log the exception, stop display of that widget and allow the page to continue.
E.g. You have a weather widget on each page that gets data via a web service. If the web service sends you malformed XML, you would like the site to still be available.
Potential solutions:
Plan A) Within each widget, wrap contents of the init or display function in a Try-Catch statement. Plan B) Create a new IRobustWidget interface which extends IWidget, and do the Try-Catch in there.Both of these options seem a little "dirty" to me, as I've read that generic catch (exception)
statements are a bit of an anti-pattern. I'm leaning towards Plan B, as this seems more modular.
- Have I missed any other options?
- Is a generic Try-Catch ok in this regard?
The fact that you are loading the data via a web-service makes me wonder whether you should perhaps load this data async, via either an ajax-loaded <div>
, or an <iframe>
; i.e. the widget(s) is(/are) downloaded in separate requests. This gets you two advantages:
- the core page is not delayed at all by obtaining the data - that should load briefly after the main page does
- if the panel fails to load.... meh (assuming it is nice-to-have-but-not-critical)
The disadvantage is an additional round-trip, but by comparison to everything else happening, this doesn't necessarily pose a problem.
I've found what I was after- Catching unhandled exceptions in ASP.NET UserControls
I also realise that I need to decide which event I need to focus on (Render, Load or a custom SetDisplay function)
精彩评论