如何在没有堆栈跟踪的情况下创建/抛出异常? [英] How can an Exception be created/thrown with no stack trace?
问题描述
今天我正在浏览一些日志并遇到一个奇怪的错误。
I was trawling through some logs today and came across a strange error.
以下是它在日志中的显示方式:
Here's how it appears in the log:
2014/09/11 15:23:52.801 [CC3A5FDD16035540B87F1B8C5E806588:<removed>] WARN a.b.c.Ddd - Main failure
java.lang.NullPointerException: null
2014/09/11 15:23:52.801 [CC3A5FDD16035540B87F1B8C5E806588:<removed>] ...
以下是代码的样子:
} catch (Exception e) {
Ddd.log.warn("Main failure ", e);
throw e;
}
如果重要的话,代码在jsp中。在日志中再次重复相同的异常(正如您对 throw e
所期望的那样)。
The code is in a jsp if that is important. The same exception is repeated once more in the log (as you'd expect from the throw e
).
我没有原因记录 - 日志中的前一行显示了查询的执行情况。这种情况在4天内仅发生两次,似乎没有对系统造成任何损害。
I have no record of what the cause was - the previous line in the log shows execution of a query. This occurred just twice over a 4-day period and seems not to have caused any harm to the system.
环境:使用Java 5在Tomcat下运行相当繁忙的Web服务。
Environment: Fairly busy web service running under Tomcat with Java 5.
我不是在询问有关调试系统的提示 - 这些错误很快就会消失,甚至可能再也不会发生。关于如何在没有堆栈跟踪的情况下创建任何异常(尤其是NPE),我感到很难过?
I am not asking for tips on debugging the system - these errors are long-gone and may even never happen again. I was just stumped as to how any exception (especially an NPE) could be created without a stack trace?
已添加
正在使用的记录器是 slf4j
驱动 Logback
实例。我相信警告
方法是此处。不确定解析为的 Logback
方法,但我相信 Throwable
参数是专门处理的,如果有堆栈的话附加到 Throwable
的跟踪将显示在日志中。
The logger being used is an slf4j
driven Logback
instance. I believe the warn
method is here. Not sure what Logback
method that resolves to but I am confident the Throwable
parameter is treated specially and if there was an stack trace attached to the Throwable
it would appear in the log.
LogBack.xml - 按要求:
LogBack.xml - as requested:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="log.package" value="Package" />
<property name="log.pattern" value="%date{yyyy/MM/dd HH:mm:ss.SSS} [%X{session}:%X{device}] %level %logger{25} - %msg%n"/>
<property name="log.consolePattern" value="%highlight(%-5level) %white(%logger{25}) - %msg%n"/>
<if condition='isDefined("catalina.home")'>
<then>
<property name="log.dir" value="${catalina.home}/logs" />
</then>
<else>
<property name="log.dir" value="." />
</else>
</if>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<encoder>
<Pattern>${log.consolePattern}</Pattern>
</encoder>
</appender>
<appender name="rolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.dir}/${log.package}.log</file>
<encoder>
<Pattern>${log.pattern}</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.dir}/${log.package}.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 16MB. -->
<maxFileSize>16MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- Keep no more than 3 months data. -->
<maxHistory>90</maxHistory>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
</appender>
<!-- Default logging levels. -->
<root level="INFO">
<appender-ref ref="console"/>
<appender-ref ref="rolling"/>
</root>
<!-- Specific logging levels. -->
<!-- Generally set to INFO or ERROR but if you need more details, set to DEBUG. -->
<logger name="org.apache" level="INFO"/>
<logger name="net.sf.ehcache" level="ERROR"/>
<logger name="com.zaxxer" level="ERROR"/>
<logger name="ch.qos" level="ERROR"/>
</configuration>
我手工编辑了日志中会话ID之后的值,以删除客户数据。
I hand-edited out the values after the session ID in the log to remove customer data.
推荐答案
有时候,尤其是在NullPointers(根据我的经验)中,jvm可以优化异常的创建和转换,以及堆栈跟踪丢失(或更准确,从未创建)。我怀疑你的问题与某些java库无关,而是与jvm本身有关。
Sometimes, especially when it comes to NullPointers (in my experience), the jvm can optimize the creation and casting of exceptions, and the stack trace is lost (or more correctly, never created). I suspect that your issue is not related to certain java libs, but rather the jvm itself.
如果在启动jvm-process时添加此参数,您将得到堆栈如果我的怀疑是正确的话,追溯回来。
If you add this argument when starting your jvm-process you will get your stack traces back, if my suspicion is correct.
-XX:-OmitStackTraceInFastThrow
之前已经询问过,请点击此处了解更多详情:
This has been asked before, look here for more details:
- Java中没有StackTrace的没有StackTrace的Java中的NullPointerException
- 重复异常没有堆栈跟踪 - 如何重置?
- NullPointerException in Java with no StackTrace
- Recurring Exception without a stack trace - how to reset?
注意,这适用于sun / oracle jvm
Note, that this applies sun/oracle jvm
这篇关于如何在没有堆栈跟踪的情况下创建/抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!