在ServletContextListener中处理Java异常的首选方法 [英] Preferred way to handle Java exceptions in ServletContextListener

查看:72
本文介绍了在ServletContextListener中处理Java异常的首选方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于servlet生命周期的东西,你们建议你做什么来回应异常......

For servlet lifecycle stuff, what do you guys recommend doing in response to an exception...

例如,

public class Foo implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        try {
           // something nasty
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        try {
           // something nasty
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

我不完全确定将处理运行时的内容以上例外。我的想法是,如果在这里抛出异常,它们就足以严重破坏系统,因此(未处理的)运行时异常可能可以。

I'm not entirely sure what will handle the runtime exception above. I'm working from the idea that if exceptions are thrown here, they're serious enough to break the system completely so an (unhandled) runtime exception may be ok.

我想我问的是从servlet上下文监听器处理未经检查的异常是什么?

I guess I'm asking what handles unchecked exceptions from servlet context listeners?

推荐答案

当你抓到例外情况,您可能需要考虑设置 ServletContext 属性以指示遇到错误。这样,如果容器没有禁用该应用程序,您可以过滤器和/或 Servlet 检查 ServletContext 属性并采取适当的操作,例如显示错误页面。

When you catch an exception, you might want to consider setting a ServletContext attribute to indicate that an error has been encountered. That way, if the container has not disabled the app, you can have Filters and/or Servlets inspect the ServletContext attribute and take appropriate action, like displaying an error page.

这篇关于在ServletContextListener中处理Java异常的首选方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆