Struts2 中处理异常的最佳方法?
谁能告诉我在 Struts2 中处理异常的最佳方法?我已经在 struts.xml 中为特定异常配置了全局异常映射。我找不到处理异常的最佳方法。我想到了两种方法。 1) 在每个方法中放入 try/catch 并从 catch 块中抛出异常 2) 不捕获任何异常,即在方法的 throws 子句中声明异常,以便 Framework 自动处理异常并从 struts.xml 选择合适的映射并显示相应的错误页。
如果有人知道或有关于此异常处理的教程链接,请提供给我。
Can anybody tell me the best approach of handling exceptions in Struts2? I've configured global exception mappings in struts.xml for specific exceptions. I couldnt find the best approach of handling exceptions.I have two approaches in mind.
1) putting try/catch in each method and throwing exceptions from catch block 2)Without catching any exceptions ie., declaring exceptions in throws clause of method so that Framework automatically handles exceptions and picks suitable mapping from struts.xml and displays the respective error page.
If anybody knows or has tutorial link on this exception handling, pls provide me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
捕获并随后重新抛出异常几乎从来都不是一个好主意。通常,只有当您实际上可以对捕获的异常执行某些操作时,您才需要 try/catch 块,例如处理它并正确继续,或者向异常添加其他信息。
向 struts 方法添加 throws 是完全可以接受的,如果它抛出的内容不是您可以在代码中处理的内容,而您只需要显示一个错误页面。除非 try/catch 的开销有一些增加,否则最好让它自行展开。
Catching and subsequently rethrowing an exception is almost never a good idea. Typically, you only want to have try/catch blocks if there is actually something you can do with the caught exception, such as handle it and move on correctly, or adding additional information to the exception.
Adding throws to a struts method is perfectly acceptable, in the event that what it throws is not something you can handle in your code and you just need to display an error page. Unless there is some gain to the overhead of the try/catch, it is better just to let it unwind on out on it's own.