Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Exception doubt

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the following piece of code carefully.


An attempt to compile the above class definition ...


Will cause a compiler error - non-abstract classes cannot be extended to abstract classes.

Will cause a compiler error - a constructor must be provided which may or may not throw an IOException

will cause a compiler error - a constructor must be provided which must throw an IOException or one of its super types.-answer

will not cause any compiler error. The class definition is perfectly legal.


IOException's sub type or super type. I think the extended class can throw the original type or sub type of exception from it's super class.
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
The subclass constructor must throw IOException or supertype

consider these two method and try to link with constructor
class A
{
void met1() throws IOException
{
throw new IOException();
}
}
class B
{
void met2() throws FileNotFoundException
{
met1();
}
}

now met2() will not be able to handle IOException
therefore it should be
void met2() throws Exception(IOException)
{
met1();
}
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But you should write (IOException) instead of (Exception)
 
V Gala
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
both exception and ioexception can catch IOException
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
doll, its like saying...



and there it blows up!
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dolly,

if you say "IOException's sub type" then just ask a question,

How would a sub-type handle a super type exception..

A simple as that..

Also it is clearly mentioned in K&B book regarding the same

Regards,
Gitesh
 
I am a man of mystery. Mostly because of this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic