• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Exception

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across a question :
import java.io.*;
public class TechnoSample
{
public static void main(String[] args)
{
TechnoSampleSub myref = new TechnoSampleSub();
try
{
myref.test();
}
catch(IOException ioe){}
}

void test() throws IOException
{
System.out.println("In TechnoSample");
throw new IOException();
}
}
class TechnoSampleSub extends TechnoSample
{
void test()
{
System.out.println("In TechnoSampleSub");
}
}
When I compiled this , it gave me a compile-time error, but after I changed all IOException in the three places to Exception, it compiled and ran.
I know that when there was a compile error, it is due to the test() method in the subclass never throws a IOException, but I think after changed to Exception, the problem was still in existence.
Thanks Yi
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yi
It is because you need not guard for Exception but for all its subclasses. As IOException is a subclass Exception it gives you an error. That is checked exception rules apply to all subclasses of Exceptions except Runtime Exceptions.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anupam Sinha:
Hi Yi
It is because you need not guard for Exception but for all its subclasses. As IOException is a subclass Exception it gives you an error. That is checked exception rules apply to all subclasses of Exceptions except Runtime Exceptions.


Sorry anupam.. didn't get that one.. could you re-explain again
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andres
Yes my earlier post was, I guess confusing, Sorry.
OK let's take Yi's example.
When you are throwing a checked exception you need to declare that the method may/will throw a checked exception or you need to have try and catch blocks. Similarly when you are catching a checked exception there should be a probability that the exception may arise at the first place.
In Yi's Example

this will give an error as IOException is being caught and there is no place from where the exception might probably arise. But this rule only applies to checked exceptions. But it does not apply to the Exception class itself. This is probably because the Exception class is also the parent class for the RuntimeException class. So it means that if you just want to throw a RuntimeException (or it's subclass) you can use a catch to handle all RuntimeException you are throwing or all those RuntimeException's that may arise using only catch(Exception e){}.
Hope this time I am clear. If in doubt repost.
[ June 29, 2003: Message edited by: Anupam Sinha ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks anupam..


...this will give an error as IOException is being caught and there is no place from where the exception might probably arise.



This code compiles OK, even though test() will never throw IOException.
I think I'm missing something here.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andres
Exactly in your code the IOException will not arise. Now if the IOException will never be thrown then there is no need for the method signature to be like void test() throws IOException. But if that is removed then you will get a compiler error. The throws clause suggests that the method or its overriding methods may throw the exception(or a subclass of the checked exception). The possible reason for this is probably that at compile time it is not possible to tell as to whether if the reference variable is pointing to a subclass or the original class so the compiler allows this.
[ June 29, 2003: Message edited by: Anupam Sinha ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andres
You may also find these links to be helpful.
link1 and link2
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, just an FYI for exam questions:
Even if a method explicitly declares that it throws a type of RuntimeException, you are NOT required to have a try/catch block because it is not a checked exception.
reply
    Bookmark Topic Watch Topic
  • New Topic