在struts的action中throw exception却没有打印到控制台

来源:互联网 发布:足彩数据分析 编辑:程序博客网 时间:2024/06/05 09:06

在struts的action中throw exception却没有打印到控制台

问题: 在struts的action中throw出来的异常,在jsp上显示了,但是没有打印到控制台。

结论:先说下吧,到最后还是没有找出来为什么没有打印异常。

下面是寻找为什么打印的过程,一步一步自己还是学到了一些东西。

  1. JVM为什么能打印出来我们throw出来的异常。
    我们在类中抛出来的异常,如果没有指定uncaughtExceptionHandler,则默认调用ThreadGroup的uncaughtException的方法,先去找parent group,如果存在就继续向上走。如果没有parent了,还是没有这种类型异常的处理器,那么就是打印到system.err中.

    Called by the Java Virtual Machine when a thread in this thread group stops because of an uncaught exception, and the thread does not have a specific Thread.UncaughtExceptionHandler installed.

    The uncaughtException method of ThreadGroup does the following:

    If this thread group has a parent thread group, the uncaughtException method of that parent is called with the same two arguments.
    Otherwise, this method checks to see if there is a default uncaught exception handler installed, and if so, its uncaughtException method is called with the same two arguments.
    Otherwise, this method determines if the Throwable argument is an instance of ThreadDeath. If so, nothing special is done. Otherwise, a message containing the thread’s name, as returned from the thread’s getName method, and a stack backtrace, using the Throwable’s printStackTrace method, is printed to the standard error stream.
    Applications can override this method in subclasses of ThreadGroup to provide alternative handling of uncaught exceptions.

    Specified by: uncaughtException(…) in UncaughtExceptionHandler
    Parameters:

    • t the thread that is about to exit.
    • e the uncaught exception.
  2. 那么Struts对抛出来的异常是怎么处理的呢?
    从开始调用,进入ExceptionMappingInterceptor拦截器

    1. 首先try catch捕捉到异常
    2. 在catch过程中处理,查看是否设置了打印异常及级别;如果设置了就打印异常。
    3. 将exception根据配置匹配并跳转到特定的global error页面
    4. 如果匹配结果为空,那么就throw e
    5. 关键这里也没有进入ThreadGroup的uncaughtException方法,这就很奇怪了

总结一下:

  1. 我在ThreadGroup中打了断点,普通的main可以进去,但是action抛出来的进不去。
  2. 不是log4j或者其他引起的,因为断点都没进
  3. 在action中e.printStacktrace()可以成功打印异常,显示调用printStacktrace(System.err)

推测:

action抛出来的异常不能被ThreadGroup的uncaughtException处理,可能有设置过默认的default uncaughtException handler,也可能不让默认的进行处理。希望有知道的可以给我讲解一下.

解决办法:

  1. 显示调用e.printStackStrace
  2. 配置exceptionInterceptor
  3. 将action抛出来的异常,整体进行一层的捕获,形成更友好的信息。
0 0
原创粉丝点击