Seemingly puzzling exception handling behavior that appears sensitive to try block content #3443
-
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 1 reply
-
Perhaps the title should read " ... depending on try block content ..." ?? Please always provide proper code with imports, class headers and such 😧 - Thanks! ECJ behavior compares well with Javac 23 😄 Compilers actually track what exceptions may be raised by the try block, so can speak of "sharper" types -- that explains the puzzle. I would say there is some mild surprise with the quick fixed added throws clause, although it is not a correctness issue and perhaps a long standing behavior. There is some surprise why the catch block wouldn't be warned about in the very first case - but given bother compilers agree there is some arcane provision in the spec that calls for it, I would daresay In summary, I don't see a defect here. |
Beta Was this translation helpful? Give feedback.
-
FileOutputStream("") can only throw FileNotFoundException so it may be surprising but not an error |
Beta Was this translation helpful? Give feedback.
-
Here is my take for a plausible explanation without poking into JDT.UI code: This is very likely a case of the left hand not knowing what the right is doing. Quick fix is implemented by JDT.UI perhaps by scanning the catch blocks in existence disregarding the _ textual contents_ of the compiler message. I would imagine that JDT.UI only looks at @noopur2507 may be able to confirm. |
Beta Was this translation helpful? Give feedback.
-
@jukzi - hope you don't mind, edited the title further to clarify. |
Beta Was this translation helpful? Give feedback.
-
Sorry for that I just don't wanted to clutter the code and though a simple main class does not leave much problem for any JDT committers :-)
I don't understand this comment, no one said that it is an error, I just used that as an example for a different Exception type thrown by a method.
It was jsut very surprising as I edited some old code and suddenly got the message to throw exception but if it is desired I maybe just don't use that pattern often enough to ever have noticed that :-D |
Beta Was this translation helpful? Give feedback.
-
@srikanth-sankaran maybe this is better converted to a Q/A discussion then instead of closing the issue. |
Beta Was this translation helpful? Give feedback.
-
We always prefer full but minimal self standing examples. That would allow copy + paste + save + run javac to compare behavior. And eliminate any and all guesswork and collateral surprises. |
Beta Was this translation helpful? Give feedback.
-
OK, consider this program:
This is rejected by both javac and ECJ with near identical message about But there is no complaint about the other catch block that catches This is the first mystery - right? JLS 14.22 Unreachable Statements reads ... So a straightforward conclusion is that JLS mandates So I think both mysteries are explained. One by JLS citation and the other by a plausible left hand not knowing right hand's action claim :) Let me know if you have more questions |
Beta Was this translation helpful? Give feedback.
Perhaps the title should read " ... depending on try block content ..." ??
Please always provide proper code with imports, class headers and such 😧 - Thanks!
ECJ behavior compares well with Javac 23 😄
Compilers actually track what exceptions may be raised by the try block, so can speak of "sharper" types -- that explains the puzzle.
I would say there is some mild surprise with the quick fixed added throws clause, although it is not a correctness issue and perhaps a long standing behavior.
There is some surprise why the catch block wouldn't be warned about in the very first case - but given bother compilers agree there is some arcane provision in the spec that calls for it, I would daresay
I…