• 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

confusion

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i run the program below it gives compiler error saying that myref.test() does not throw exception.but as i know it is not necessary that the code should throw the exception


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");
}
}
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed. The test method you call does not declare it throws IOException, so trying to catch this exception will result in an "unreachable catch block" compiler error. Is this what you wanted to know?
 
Rippon Jalali
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But whenver we write a try catch block.It is not necessary that exception will occur .It may or may not occur.
The error comes:
t.java:7: exception java.io.IOException is never thrown in body of corresponding
try statement
}catch(IOException ioe){}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it's not true that it may or may not occur. It will not occur, since your reference variable is of type TechnoSampleSub, and TechnoSampleSub.test() does not declare the exception. If the reference variable were of type TechnoSample, then it would be a different story.
 
Rippon Jalali
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But as shown in the simple program below,never a exception can be thrown in try block.BUt prog compiles fine and runs


public class t {
public static void main(String[] args) {
int i = 2;
try {
i=9;

} catch (Exception ae){
System.err.println(" exception caught");
}
System.out.println(i);
}
}
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

Originally posted by Rippon Jalali:
But as shown in the simple program below,never a exception can be thrown in try block.BUt prog compiles fine and runs


public class t {
public static void main(String[] args) {
int i = 2;
try {
i=9;

} catch (Exception ae){
System.err.println(" exception caught");
}
System.out.println(i);
}
}




yes , above code work well. because it catch Exception class not the special kind of exception classes. you can not catch IOException if your block not having IO operation statements. Like wise you can not catch SQLException if your block not having SQL operations statements.

milan.
 
Rippon Jalali
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but even if you throw exception other than general 'Exception' like 'ClassCastException'.Still it doesn't throw exception.
 
Rippon Jalali
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"sorry the previuos one had a mistake".I am repeating again.But even if you throw exception other than general 'Exception' like 'ClassCastException'.Still it doesn't catch that exception.
 
Milan Jagatiya
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because they are runtime unchecked exceptions. and IOException is compile time. so compiler never allow you to throw if there is no IO statements.

milan.
 
Rippon Jalali
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all of your help.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, let's try to explain it: You are free to "catch" all unchecked exceptions and errors, even Throwable. But if you want to "catch" checked exceptions, there are some restrictions.
1st, it's no problem to catch an exception using "catch (Exception e)".

2nd, when you want to catch a special checked exception (subclass of Exception), the compiler will check if the try-block "is able" to throw this special exception (or a subclass of it).

This is perfectly legal. The code in the try-block throws an IOException, so you can catch it.
Perfectly legal, too. The method foobar() declares that it might throw an IOException (though it never does), so, for the compiler, the code in the try-block is able to throw an IOException.
Not legal, because foobar() is unable to throw an IOException.

Hope this helps a bit...
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was a extermely good explanation.Thanks a lot.all my doubts are cleared now.
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic