ObjectDatasource的Select方法抛出异常如何处理?
我有一个连接到 ObjectDatasource 的 Select 方法,该方法可能会引发异常,但我不知道如何处理它!
问题是我无法控制它。当页面呈现时,ObjectDatasource 直接调用 select 方法,并直接抛出未处理的异常。
另一方面,如果有问题,我不想让它返回空集合,因为集合可能是空的,没有问题。
那么,哪里可以处理异常呢?
还有其他选择吗?
I have a Select method connected to an ObjectDatasource, this method might throw an exception and I don't know how to handle it!
The problem is that I'm not controlling it. When the page is rendered then the select method is called directly by the ObjectDatasource and an unhandled exception is thrown directly.
On the other hand, I don't want to make it return empty collection if it has a problem because the collection might be empty without problems.
So, where can I handle the exception?
Any other options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 ObjectDataSource 上的 eventargs。应该有一个 e.Exception & e.您可以查询选择成功/错误的结果。
Look at the eventargs on the ObjectDataSource. There should be an e.Exception & e.Results that you can query for the success/error of your select.
您应该订阅
ObjectDataSource.Selected
事件。正如 @Kirill 提到的那样,检查该事件中的异常,并可能隐藏 gridview 并向用户显示一些错误消息。检查此链接。
You should subscribe to the
ObjectDataSource.Selected
event.Check for exception in that event as @Kirill mentions and probably hide the gridview and display some error message to the user. Check this link.
如果我理解正确的话,您有一个页面在某个时刻调用 ObjectDataSource 上的
Select()
,并且此调用有时会失败并出现异常。现在,您处理此异常的位置在某种程度上取决于您的场景。一般来说,您应该尽早尝试处理异常有意义的,也就是您可以做一些有用的事情来响应错误。例如,对于一个可能可以将用户重定向到错误页面的网站。
请注意,虽然这个有意义的早期点实际上可能已经很晚了,如果您将用户重定向到错误页面,它可能与 ui(或页面)层一样高。您可能会在早些时候尝试捕获异常并重试请求,如果失败,则重新抛出异常
抱歉 vauge awnser,但这实际上取决于:)
If i understand correctly, you have a page that at some point calls
Select()
on a ObjectDataSource and that this call sometimes fails with an exception.Now where you handle this exception is somewhat dependant on your scenario. in general you should try and handle exceptions at the earliest point where it makes sense, that is where you can do something useful in response to the error. For an website that might be at a point where you can redirect the user to an error page for example.
Note though that this early point where it makes sense might actually be quite late, in case you're redirecting the user to an error page, it might be as high up as the ui(or page) layer. You might at some earier point try to catch the exception and retry the request and if that fails, rethrow the exception
Sorry for the vauge awnser, but it really depends :)