Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
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
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Programmer Certification (OCPJP)
Overriding methods with exception.
Tom Hass
Greenhorn
Posts: 5
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Guys,
package Vehicle; import java.io.*; public class Vehicle{ public void demo() throws IOException{ } public static void main(String[] args){ Vehicle VV = new Vehicle(); Vehicle VC = new Car(); Car CC = new Car(); VV.demo(); CC.demo(); } } class Car extends Vehicle{ public void demo(){ try{ File F = new File("Vehicle.txt"); throw new IOException(); } catch(IOException E){ E.printStackTrace(); } } }
I was practising with the following code but had a problem during compilation. The compiler says;
"error: unreported exception IOException; must be caught or declared to be thrown".
Please what could be the problem here?
Sarah Rising
Ranch Hand
Posts: 38
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Tom,
The line: VV.demo();
needs to be replaced with something like:
try { VV.demo(); } catch(IOException ex){ // process IOException }
because you've declared that Vehicle.demo() throws IOException
Hope that helps.
Sarah
Tom Hass
Greenhorn
Posts: 5
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Ok. Thanks Sarah.
Justin Allen
Greenhorn
Posts: 18
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You can also "duck" the exception if you would like by declaring that the main function throws IOException like this:
package Vehicle; import java.io.*; public class Vehicle{ public void demo() throws IOException{ } public static void main(String[] args) throws IOException{ Vehicle VV = new Vehicle(); Vehicle VC = new Car(); Car CC = new Car(); VV.demo(); CC.demo(); } } class Car extends Vehicle{ public void demo(){ try{ File F = new File("Vehicle.txt"); throw new IOException(); } catch(IOException E){ E.printStackTrace(); } } }
Tom Hass
Greenhorn
Posts: 5
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Cheers Justin. I overlooked the main method. Silly me.
yusuf Kaplan
Ranch Hand
Posts: 59
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I recommend to try also VC.demo() to get a deeper understanding of
polymorphism
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
casting
superclass and subclass
Upcasting from Class to interface Problem
DOUBT on DOWNCASTING
Casting
More...