Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Compiler error complaining about the catch block where no IOException object can ever

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer to this below question is a>.But i am unable to understand this why a is the answer that is Compiler error complaining about the catch block where no IOException object can ever be thrown.please explain me why ?

What is the result of the following code?

01: public class AQuestion {
02:
03: public static void main(String args[]) {
04:
05: System.out.println("Before Try");
06:
07: try {
08: }
09: catch(java.io.IOException t) {
10: System.out.println("Inside Catch");
11: }
12:
13: System.out.println("At the End");
14: }
15: }


a> Compiler error complaining about the catch block where no IOException object can ever be thrown.
b> Compiler error - IOException not found. It must be imported in the first line of the code.
c< The lines "Before Try" and "At the end" are printed on the screen
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to the question is B.
When you compile the code

Since IOException is a checked exception if the class IOException must first be imported , But once you place import java.io.IOException you will get compiler error saying "Try block does not throw IOException and hence you should not have.So after you import the answer to the question is a.
Hope this clears.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak,

I tried the following and in both cases the compiler doesn't complain:

1. declaring only Exception instead of the IOEXception:
try{
}catch(Exception t)
{
System.out.println("Inside catch");
}

2.try{
System.in.read();
}catch(java.io.IOException t)
{
System.out.println("Inside catch");
}

So what you mean to say is, because IOException is a checked Exception, we have to have code in the try block that might throw an IOException?
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i agree with Niara Bhonde but why we have to require a statement which might throw an IOException in try block while catching IOException But this is not the case with other exceptions.
Why the code 1. given below is compile but 2.will show compiler errorcPlease explain me ?

1. declaring only Exception instead of the IOEXception:
try{
}catch(Exception t)
{
System.out.println("Inside catch");
}

2.try{
}catch(java.io.IOException t)
{
System.out.println("Inside catch");
}
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The reason catching Exception compiles, is because Exception (besides being a superclass of IOException), is also the superclass of RuntimeException. This means that unchecked exceptions may also be caught.

Henry
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to you All VERY VERY MUCH to clear my doubt regarding this catching of checked exception.
 
We don't have time to be charming! Quick, read this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic