JavaExample.java:7: error: unreported exception Exception; must be caught or declared to be thrown
example.m1();
No, that is completely wrong. An overriding should be a feasilbility‑preserving refinement, so for all circumstances in which the superclass' method is feasible, the subclass' method should be feasible too. So the subclass' method should never throw any Exceptions which the superclass method does not throw. I think that you will understand your four “rules” as one rule with “not” in, which I can write formally like this:-prateek shaw wrote:. . . 1-You can throw any new unchecked exception which is not thrown by base method. I agreed with this point 100%. Advantage . . .
There is a difference between something which compiles because the compiler does not notice an error and something which is correct code.The compiler does not notice unchecked exceptions.
Campbell Ritchie wrote:
∀e • e ∈ Exceptions ⇒ ¬canThrow(superclassMethod, e) ⇒ ¬canThrow(subclassMethod, e)
Remember that ⇒ associates to the right, and you can use the contrapositive rule to turn the right part of that implication round:-
p ⇒ q ≡ ¬q ⇒ ¬p
∴ ¬canThrow(superclassMethod, e) ⇒ ¬canThrow(subclassMethod, e) ≡
canThrow(subclassMethod, e) ⇒ canThrow(superclassMethod, e)
"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
1-You can throw any new unchecked exception which is not thrown by base method. I agreed with this point 100%. Advantage . . .
The compiler does not notice unchecked exceptions.
∀e • e ∈ Exceptions ⇒ ¬canThrow(superclassMethod, e) ⇒ ¬canThrow(subclassMethod, e)
Remember that ⇒ associates to the right, and you can use the contrapositive rule to turn the right part of that implication round:-
p ⇒ q ≡ ¬q ⇒ ¬p
∴ ¬canThrow(superclassMethod, e) ⇒ ¬canThrow(subclassMethod, e) ≡
canThrow(subclassMethod, e) ⇒ canThrow(superclassMethod, e)
J. Kevin Robbins wrote:Thank you. I actually understand that explanation. I wish I had learned calculus. It seems fascinating. I recall seeing that scene in "The Day the Earth Stood Still" where Keanu Reeves and John Cleese are scribbling equations on the board as if they are having a conversation in numbers. Is that really possible?
Propositional calculus, not differential or integral calculus, which are different. And of course it is not complicated. Otherwise I would not have understood itStephan van Hulst wrote:This is propositional calculus. It's actually not too complicated. . . .
It is like the difference between driving through town at 78mph and driving through town at 78mph and passing a traffic policeman. The outcome might be the same in both cases that you will cause a nasty accident. But in the second case (policeman) you will slow down to a safer speed to avoid getting caught. It does not make it better or worse whether the policeman is there to notice or not. It is the responsibility of the driver to drive at a safe speed.prateek shaw wrote:. . . as common and normal mind thinking i can say both outcome is same. . . .
The semantics of the ⇒ operator in logic are that it is only false if you have true to its left and false to its right. If the subclass doesn't throw an exception (false on the left), then the whole expression is true, irrespective of what is on the right of the ⇒. That means that if the subclass method doesn't throw an Exception, then it doesn't matter whether the superclass does or does not throw an exception. Let's turn that round, so everybody other than Stephan and me can understand it:-A few hours ago, I wrote:. . . canThrow(subclassMethod, e) ⇒ canThrow(superclassMethod, e). . .
Unchecked exceptions: these are like driving. It is the driver's responsibility to drive safely, so it is the programmer's responsibility to avoid unchecked exceptions as far as possible.If the superclass method might throw an Exception, it is all right for the subclass method not to throw that Exception. If the superclass method doesn't throw an exception, then the subclass method doesn't throw that exception.
...and call you things they might print on other websites He is right. You don't want unexpected Exceptions. It will annoy him even more if you say, “Well, it was an unchecked Exception and the code compiled all right.”All other equals methods accept null as an argument. Why don't you?
Well, at least that is the theory. You can read about it in the Java™ Tutorials. Remember, this is a controversial feature; if you search for “Checked Exceptions. An experiment which failed.”, you will find a completely different opinion.A few minutes ago, I wrote:. . . The javac tool is programmed on the belief that most RuntimeExceptions mean there is an error somewhere in the code . . .
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|