Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java exception not being caught

Tags:

java

exception

How can this be? Looks like plain as daylight an exception from a third party library is skipping my catch block. Not sure where to begin troubleshooting this. It's either me being really stupid, or me not understanding something subtle about exceptions and 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

like image 529
djechlin Avatar asked May 17 '12 20:05

djechlin


People also ask

What happens if exception is not caught Java?

What happens if an exception is not caught? If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console.

Which exception type Cannot be caught in Java?

Exceptions that Can't be Caught One such exception is the limit exception ( System. LimitException ) that the runtime throws if a governor limit has been exceeded, such as when the maximum number of SOQL queries issued has been exceeded.

Which exceptions Cannot be caught?

The only exception that cannot be caught directly is (a framework thrown) StackOverflowException. This makes sense, logically, as you don't have the space in the stack to handle the exception at that point.

Is it possible try without catch in Java?

Yes, It is possible to have a try block without a catch block by using a final block. As we know, a final block will always execute even there is an exception occurred in a try block, except System.


2 Answers

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.

like image 39
Miserable Variable Avatar answered Sep 30 '22 14:09

Miserable Variable


You're attempting to catch the wrong exception type.

The signature for MimeTokenStream.next() says it can throw MimeException, which you are not catching. (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.)

Now, if you look at the source code for the actual source of the exception, line 242 of MimeEntity.readRawField, you'll see:

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. 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.

like image 78
matts Avatar answered Sep 30 '22 14:09

matts