• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Exception Handling

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Qestion 10 ( from RHE chapter 5)
The method risky might throw a java.io.IOException, java.lang.RuntimeException, or java.net.MalformedURLException (which is a subclass of java.io.IOException). Appropriate imports have been declared for each of those exceptons. Which of the following classes and sets of classes are legal? (choose one or more)
D)
public class SomeClass {
public void aMethod() {
try {
risky();
}
catch (IOException e) {
e.printStackTrace();
}
}
}

public class AnotherClass extends SomeClass {
public void aMethod() throws java.io.IOException {
super.aMethod();
}
}
Why this one is correct? My confusion is that the subclass AnotherClass override the aMethod() with a throws clasue. How could the supper aMethod() with no throws clause, but it is still correct.
My understanding for a overridden method is that. The method definition in the subclass can only specify all or a subset of the exception specified in the throws clause of the overridden method in the supperclass.
if i were wrong, please correct me!

 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The compiler makes you either call the risky() method in a try block AND a catch block
OR
declare that the calling method might throw an exception.
In SomeClass the first is true and in AnotherClass the second is true.
Hope I'm right on this one.

Terry
 
eric chan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still don't get it ! can anyone explain more ?
Here is one of aspects about method overriding.
The new method definition in the subclass "can only" specify all or a subset of the exception classes specified in the throws clause of the overridden method in the supperclass.
From the about code, the aMethod in supperclass has "not" throw any exception, but in the subclass aMethod (ovrridden method)throws IOException.
So, how can that be correct? I don't think that would compile. Please correct me if i were wrong.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin
You're correct the overridding metod in the subclass can not throw any exceptions higher i the hierarchy then the method it is over riding. In this example aMethod() in SomeClass has placed the code in a try/catch block so it doesn not have to put the throws clause in its definition. Calling it from AnotherClass is also ok because the exception is fully handled in the base class. If it weren't and the base class method could throw the exception back up the call stack then it would also have to declare that it throws the exception and then it would be ok for the sub class to do the same.
The code as they have described it doesn't compile. So that answer can't be correct.
What version of the book do you have? I have the same book and don't remember anything in that chapter as odd, of course you could be more observant than I am.
Now I'll have to go home at lunch and check my book out too.

Dave
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dave Vick:
In this example aMethod() in SomeClass has placed the code in a try/catch block so it doesn not have to put the throws clause in its definition. Calling it from AnotherClass is also ok because the exception is fully handled in the base class.
Now I'll have to go home at lunch and check my book out too.
Dave


Dave, are you sure your above statement is correct, because it really contradicts what i know. But i understand your reasoning for that logic.
BTW, i checked and found the option D has no the second block of subclass. Maybe Eric mixed option D and E? Or my 2002 version RHE is dated?
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric,
The following code is answer D in my book, and it's one of the correct answers.
public class SomeClass {
public void aMethod() {
try {
risky();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
 
eric chan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank's Daniel.
I mixed up the code. The second block is only for option E ( I thought the second block is for every option at the begining).
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Daniel Wu:
Dave, are you sure your above statement is correct, because it really contradicts what i know. But i understand your reasoning for that logic.


Daniel
I'm fairly sure what I wrote is right. I ran a couple of tests yesterday when I was looking at it to make sure nd they all ran they way I thought they would. What is it you don't agree with? I may be arong or not have understood it the right way when I read it.
Dave
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic