捕获 Web 控件中未处理的异常以允许页面呈现
(我实际上正在使用 Ektron CMS,并且正在使用 Widgets
,它们是用户控件的非常简单的扩展)。
问题:我有一个包含许多小部件的网页。如果这些小部件之一发生异常,页面将停止并向用户显示服务器错误。我希望在小部件中有未处理的异常来记录异常,停止显示该小部件并允许页面继续。
例如,每个页面上都有一个天气小部件,它通过网络服务获取数据。如果 Web 服务向您发送格式错误的 XML,您希望该网站仍然可用。
潜在的解决方案:
计划 A) 在每个小部件中,将 init 或 display 函数的内容包装在 Try-Catch 语句中。
B 计划 创建一个新的 IRobustWidget 接口来扩展 IWidget,并在其中执行 Try-Catch。
这两个选项对我来说似乎都有点“肮脏”,因为我读到通用 catch(异常)
语句有点反模式。我倾向于 B 计划,因为这看起来更加模块化。
- 我是否错过了其他选择?
- 在这方面通用的 Try-Catch 可以吗?
(I am actually using Ektron CMS, and am using Widgets
, which are a very simple extension of a user control).
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您通过 Web 服务加载数据的事实让我想知道您是否应该通过 ajax 加载的
或
<; 异步加载此数据。 iframe>
;即小部件是在单独的请求中下载的。这会给你带来两个好处:缺点是需要额外的往返,但与其他发生的事情相比,这不一定会造成问题。
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 disadvantage is an additional round-trip, but by comparison to everything else happening, this doesn't necessarily pose a problem.
我找到了我所追求的东西——
捕获 ASP.NET UserControls 中未处理的异常
我也意识到我需要决定需要关注哪个事件(渲染、加载或自定义 SetDisplay 函数)
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)