Prev: Hashcode and Equal
Next: Speaking of thread safety?
From: Patricia Shanahan on 10 Mar 2010 11:55 Jeff KE7NVY wrote: > On Mar 9, 9:53 pm, Javas <deepan...(a)gmail.com> wrote: >> public class Qn24 { >> >> static class A >> { >> void process() throws Exception { throw new >> Exception(); } >> } >> >> static class B extends A >> { >> void process() { System.out.println("B "); } >> } >> >> public static void main(String[] args) >> { >> A a = new B(); >> a.process(); >> } >> >> } >> >> Why does the above code results in a compilation error? since it is >> always invoking class B's process() method which doesnt throw any >> exception. > > Exception is a checked exception. That means that whenever you > declare > a method that (potentially) throws it, you must do one of two things > in > the code that calls that method. Either handle the exception by > putting > the call inside a try/catch block: > > public static void main(String[] args) { > A a = new B(); > try { > a.process(); > } > catch (Exception e) { > System.out.println("main: Exception caught"); > } > } I don't like code that catches Exception and neither re-throws it, possibly wrapped with additional data, nor terminates the program. If this main method were called from another class, rather than being used as the top method of the whole application, the caller could be left continuing to execute with the processing not done and no indication that something went wrong other than a line in standard out. > > Or, declare the calling method as also throwing Exception: > > public static void main(String[] args) throws Exception { > A a = new B(); > a.process(); > } A third option is to change the declaration to "B a = new B();". The compiler could then use the knowledge that variable a refers to a B instance. Patricia
From: Jeff KE7NVY on 10 Mar 2010 12:19 On Mar 10, 8:55 am, Patricia Shanahan <p...(a)acm.org> wrote: > Jeff KE7NVY wrote: > > On Mar 9, 9:53 pm, Javas <deepan...(a)gmail.com> wrote: > >> public class Qn24 { > > >> static class A > >> { > >> void process() throws Exception { throw new > >> Exception(); } > >> } > > >> static class B extends A > >> { > >> void process() { System.out.println("B "); } > >> } > > >> public static void main(String[] args) > >> { > >> A a = new B(); > >> a.process(); > >> } > > >> } > > >> Why does the above code results in a compilation error? since it is > >> always invoking class B's process() method which doesnt throw any > >> exception. > > > Exception is a checked exception. That means that whenever you > > declare > > a method that (potentially) throws it, you must do one of two things > > in > > the code that calls that method. Either handle the exception by > > putting > > the call inside a try/catch block: > > > public static void main(String[] args) { > > A a = new B(); > > try { > > a.process(); > > } > > catch (Exception e) { > > System.out.println("main: Exception caught"); > > } > > } > > I don't like code that catches Exception and neither re-throws it, > possibly wrapped with additional data, nor terminates the program. If > this main method were called from another class, rather than being used > as the top method of the whole application, the caller could be left > continuing to execute with the processing not done and no indication > that something went wrong other than a line in standard out. > Absolutely true, and I agree completely. I was just trying to illustrate why the compiler was unhappy. The OP is correct in that, when you run this, B.process() will be called. But before you can run it, you have to make the compiler happy. When the compiler reaches the line a.process(); in main(), all it knows is that you declared 'a' as an A object, so it assumes that you're calling A.process(). And since A.process() potentially throws the Exception, you need to either handle it locally or pass it on up to the calling code. > > A third option is to change the declaration to "B a = new B();". The > compiler could then use the knowledge that variable a refers to a B > instance. > > Patricia
From: markspace on 10 Mar 2010 13:11 RedGrittyBrick wrote: >> You could have just answered the questions and >> stopped there, instead of worrying about whether's it my homework or >> assignment work. Or atleast you could have suggested me to name the >> class properly. But you chose the wrong path. > > http://www.catb.org/~esr/faqs/smart-questions.html#homework I don't mind answering homework questions. The more dummies who never learned to think in the class room, the more job opportunities for me.
From: Arne Vajhøj on 10 Mar 2010 19:31 On 10-03-2010 03:50, Javas wrote: > On Mar 10, 9:31 am, Lew<no...(a)lewscanon.com> wrote: >> #2: What wasn't cordial about my manner? > > You could have just answered the questions and > stopped there, instead of worrying about whether's it my homework or > assignment work. He could. If he were mean enough to do it. Nothing is more cruel than supplying the answers to homework, so that the students does not learn what they need to learn. Arne
From: Arne Vajhøj on 10 Mar 2010 19:34 On 10-03-2010 13:11, markspace wrote: > RedGrittyBrick wrote: >> http://www.catb.org/~esr/faqs/smart-questions.html#homework > > I don't mind answering homework questions. The more dummies who never > learned to think in the class room, the more job opportunities for me. Tsk tsk Arne
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Hashcode and Equal Next: Speaking of thread safety? |