• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

A doubt in Exception

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
Hope all of you are fine.
I've a small doubt in Exceptions(actually in a question from Kathy Sierra's book, Q3 in CH-5 in SCJP1.5).
Following is the program:

import java.io.*;

class Master {
String doFileStuff() throws FileNotFoundException { return "a"; }
}

class Slave extends Master {
public static void main(String[] args)
{
String s = null;
try
{
s = new Slave().doFileStuff();
}
catch ( Exception x)
{
s = "b";
}
System.out.println(s);
}
// following line irks me -->
String doFileStuff() throws NumberFormatException { return "b"; }

}

In above program class Slave overrides the doFileStuff() method of its superclass Master.
But is it a legal override?
I've checked it compiles fine but I've following doubt:
NumberFormatException is not a subclass of FileNotFoundException.
According to KS book, pg 102 says:
"The overriding method must NOT throw checked exceptions that are new
or broader than those declared by the overridden method. For example, a
method that declares a FileNotFoundException cannot be overridden by a
method that declares a SQLException, Exception, or any other non-runtime
exception unless it's a subclass of FileNotFoundException."
What I think is that the NumberFormatException is a newer checked Exception in this case and this should not be allowed by the compiler?
Please correct me where I am wrong and provide an explanation for this.
Thanks a lot friends,
Nitin
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi nitin,

NumberFormatExcpetion is a subclass of Runtime Exception. So it doesn't matter whether you include it in throws or not.

try switching your exceptions in methods.

Cheers,
Nitin
[ July 02, 2007: Message edited by: nitin pokhriyal ]
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nitin Bhardwaj:
"The overriding method must NOT throw checked exceptions that are new
or broader than those declared by the overridden method.

NumberFormatException is an unchecked exception.
 
Nitin Bhardwaj
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh Thanks Nitin and Manfred,
I just didn't pay attention to the full heirarchy due to which it went unnoticed by me that NumberFormatException is a subtype of RuntimeException.
Only checked exceptions need to be handle or declared and hence this NumberFormatException(which is an uncheked exception) will not fall under that purview.
Thanks a lot !!
Nitin

[ July 02, 2007: Message edited by: Nitin Bhardwaj ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic