简体   繁体   English

Java异常没有被捕获

[英]Java exception not being caught

How can this be? 怎么会这样? Looks like plain as daylight an exception from a third party library is skipping my catch block. 看起来像白天一样,来自第三方库的异常正在跳过我的catch块。 Not sure where to begin troubleshooting this. 不知道从哪里开始排除故障。 It's either me being really stupid, or me not understanding something subtle about exceptions and Java. 这要么是我真的很愚蠢,要么我不理解关于异常和Java的微妙之处。

My console: 我的控制台:

Exception: org.apache.james.mime4j.io.MaxLineLimitException: Maximum line length limit exceeded; stack: org.apache.james.mime4j.stream.MimeEntity.readRawField(MimeEntity.java:242); org.apache.james.mime4j.stream.MimeEntity.nextField(MimeEntity.java:258); org.apache.james.mime4j.stream.MimeEntity.advance(MimeEntity.java:296); org.apache.james.mime4j.stream.MimeTokenStream.next(MimeTokenStream.java:360); me.unroll.scanner.Scanner.<init>(Scanner.java:206); me.unroll.scanner.ScannerThread.run(ScannerThread.java:205); java.lang.Thread.run(Thread.java:722)

Problem is my code looks like this: 问题是我的代码看起来像这样:

try {  
//...
 for(EntityState token = messageStream.getState();
    token != EntityState.T_END_OF_STREAM;
    token = messageStream.next()) {  //this is Scanner.java:206
//...
catch(ScanComplete exc) { }
catch(MaxLineLimitException exc) { //line 282, matches "try" above
    debug("Am I getting caught?"); //no, it's not

I'm more 我更

You're attempting to catch the wrong exception type. 您正试图捕获错误的异常类型。

The signature for MimeTokenStream.next() says it can throw MimeException , which you are not catching. MimeTokenStream.next()的签名表示它可以抛出你没有捕获的MimeException (BTW if you are wondering why an exception isn't being caught, you can try catching Exception and logging the exception type to see what is actually being thrown.) (顺便说一句,如果你想知道为什么没有捕获Exception ,你可以尝试捕获Exception并记录异常类型以查看实际抛出的内容。)

Now, if you look at the source code for the actual source of the exception, line 242 of MimeEntity.readRawField , you'll see: 现在,如果您查看异常实际源代码的源代码, MimeEntity.readRawField第242 MimeEntity.readRawField ,您将看到:

241        } catch (MaxLineLimitException e) {
242            throw new MimeException(e);
243        }

So even though the console message says MaxLineLimitException , the actual exception being thrown by that method is a MimeException . 因此,即使在控制台消息 MaxLineLimitException ,实际的异常被抛出通过该方法是一种MimeException Try catching MimeException in your code instead of MaxLineLimitException , but beware that MimeTokenStream.next() can probably throw MimeException for other reasons besides the one you've encountered. 尝试在代码中捕获MimeException而不是MaxLineLimitException ,但要注意MimeTokenStream.next()可能因为除了你遇到的原因之外的其他原因而抛出MimeException

One possibility is that the exception was logged, subsequently caught and not rethrown. 一种可能性是记录了异常,随后被捕获而不是重新抛出。 You can put a breakpoint on the exception itself and step out from there until you reach your method. 您可以在异常本身上放置一个断点,然后从那里走出来,直到找到您的方法。

'Miserable Variable' is probably right. “悲惨变数”可能是正确的。 Another option is that you have the wrong MaxLineLimitException class imported. 另一个选择是您导入了错误的MaxLineLimitException类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM