• 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

Exception doubt

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(java.io.IOException t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
1.Compiler error complaining about the catch block where no IOException object can ever be thrown.
2.Compiler error - IOException not found. It must be imported in the first line of the code.
3.No compiler error. The lines "Before Try" and "At the end" are printed on the screen.

ANS: 1

Doubt:WHY so. Is the reason for ans: A that there does not exist anything like "java.io.IOException".


pls help
regards,
gitesh
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/264749/java-programmer-SCJP/certification/Quote-your-sources
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the compiler is intelligent enough to know that there are no chance that an IOException will be thrown in your try block.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source:

Angelfire/Abhilash-Q13
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Akhilesh!!

Please can you ellaborate and Explain the above concept

Its not that i dont agree with the above given answer
but need some explanation on it!!!


Thanks in advance!!!

Preparing SCJP 1.5
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Akhilesh Trivedi:
Because the compiler is intelligent enough to know that there are no chance that an IOException will be thrown in your try block.



Hi Akhilesh,

I tried changing code to


catch(Exception t)



But then it compiled without error.

As you told compiler is intelligent to find no IOException will be thrown, then here also no Exception will be thrown. So why is the compiler not complaining now?

Pls help,
Gitesh
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now i am getting more confused
with the above concept
[ August 29, 2007: Message edited by: dhwani mathur ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiler gives error only for IOException here because it's a Checked Exception, where as Exception class is not a checked exception. For checked exceptions, compiler checks the possibility of error occurance in your code. if there's possibility and if you didn't catch such errors, it would also give error asking you to handle them.

But in this case, as your code does not deal with reading from/writing to files or any other standard input and output,it will find that you don't need to catch such an error.
You could also try including any unchecked exceptions like NullPointer Exception, ArrayIndexOutOfBoundsException in the above code; which are checked only at runtime, It doesn't give any error. But for IOException it does.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nagarajulu Aerakoni:
Compiler gives error only for IOException here because it's a Checked Exception, where as Exception class is not a checked exception. For checked exceptions, compiler checks the possibility of error occurance in your code. if there's possibility and if you didn't catch such errors, it would also give error asking you to handle them.

But in this case, as your code does not deal with reading from/writing to files or any other standard input and output,it will find that you don't need to catch such an error.
You could also try including any unchecked exceptions like NullPointer Exception, ArrayIndexOutOfBoundsException in the above code; which are checked only at runtime, It doesn't give any error. But for IOException it does.



Welcome Nagarajulu!
That was a marvellous opening.

Dhwani hope you dont need more explaination. and say thanks to nagarajulu not lalit.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the reason that your code does not compile is that to give IOException in a catch block you have to import the package java.io and it has nothing to do with checked or unchecked exceptions as explained by some of the raunchers.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anuj bhatnagar:
the reason that your code does not compile is that to give IOException in a catch block you have to import the package java.io and it has nothing to do with checked or unchecked exceptions as explained by some of the raunchers.




You are right as well but others are not wrong. It depends what exactly the "error" is... read the whole thread again or better put try compiling once by importing the pacage.and I hope you will get it.
[ August 29, 2007: Message edited by: Akhilesh Trivedi ]
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to compile the above code. However I'm getting following compiler error:
Unreatchable Catch Block.

I tried putting some IO operations inside the try block also:

try {
File obj = new File("Abc.txt");
obj.delete();
}
catch (IOException t) {
System.out.println("Inside Catch");
}
 
Lalit Bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to compile the above code. However I'm getting following compiler error:
Unreatchable Catch Block.

I tried putting some IO operations inside the try block also:

try {
File obj = new File("Abc.txt");
obj.delete();
}
catch (IOException t) {
System.out.println("Inside Catch");
}

However, still I get the same compiler error.

But when I tried to change the catch block to catch(Exception t), it works fine.

Please explain.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !!!

Thanks for correcting me Akhilesh
i have deleted my previous post

Ohhhh What a great Explanation
Thanks a lot Nagarajulu!!!
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As earlier stated in the thread by Nagarajulu Aerakoni , you need something inside the try block which is capable of throwing IOException(a checked exception). The code in the try block is:

try {
File obj = new File("Abc.txt");
obj.delete();
}



As this code does not throw IOException, you get the same error again.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the discussion above, i was able to understand the following 2 points:

1. If I catch a "checked" exception when there is no chance of occurrence of a checked exception, the compiler will kick me

2. if is not necessary to import java.io for catching an IOException.

Please correct my understanding....

Gitesh
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gitesh Ramchandani:
From the discussion above...
...
Please correct my understanding....


Hey gitesh, dont have discussions as your base and build code on them instead have your code-base strong and justify the discussions.


if (it)is not necessary to import java.io for catching an IOException.



Did you work on this?

Further,

aFileObject.delete() is not likely to throw an IOException, it would only throw SecurityException

http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#delete()

Hope this helps.
[ August 31, 2007: Message edited by: Akhilesh Trivedi ]
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Akhilesh ,

I tried importing the io package, even then it gives the compilation error "exception java.io.IOException is never thrown in body of corresponding try statement".

I got confused with this:

Originally posted by anuj bhatnagar:
the reason that your code does not compile is that to give IOException in a catch block you have to import the package java.io and it has nothing to do with checked or unchecked exceptions as explained by some of the raunchers.



regards,
Gitesh
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Gitesh

i think you must read the post by Nagarajulu,what it actualy explains,since


i tried the below shown code it does not give any compiler error




And even i got the output

as below

Before Try
At the End



I hope this helps....!!!


Preparing SCJP 1.5
[ August 31, 2007: Message edited by: dhwani mathur ]
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is another one thing to note...


 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Akhilesh,

What is to note in:




Is it different from catch(IOException t)?

Gitesh
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gitesh Ramchandani:
Akhilesh,

What is to note in:




Is it different from catch(IOException t)?

Gitesh



Yes it is different.
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you guys are doing? I become more confuse after read this post. Can anyone from bartener explain this issue?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers!

Question:
When does code like this compile, and when not???


Answert:
Code does NOT comppile when KindOfException is
1) a SUBclass of Throwable
2) a SUBclass of Exception, with of course the exception (lowercase ) of RuntimeException plus subclasses including RuntimeException itself because these are not checked.

It does compile when the exception is
A) Throwable itself (but no sub class)
B) Exception itself (but no sub class)


Reason for not compiling: The catch block can never be reached. Something like unreackeable code.

The same is true if you don't have semicolons (or any other code) in the catch block or do have some in the catch block.



Sorry that I cannot present you a citation for this as I found this out by trial an error coding (javac 1.5.0).




N) Exceptions from the center of europe are called czech exceptions.



Yours,
Bu.
 
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
Thanks a lot Hassel. Now I got it. I hope everybody now clear.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

After going through the entire thread, I got confused about two things

a) Is class Exception, an Unchecked exception? In the link given below, it mentions that class Exception is checked and I believe , since we extend class Exception usually to define our exceptions, Exception should be checked exceptionsChecked Vs UnChecked. Please clarify me on this.

b) Some where in the thread, it is mentioned that java.io.IOException is different from IOException. If we import java.io package, in what way both are different? I'm thinking that after importing the package, we need not explicitly mention the entire path for the class, i.e enough to say IOException instead of java.io.IOException. Please correct me if I'm wrong.

Lastly I would like to thank bartender, for giving a convincing reply.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a) Exception is checked. If you throw one without either declaring or handling it, the code will not compile. This is how you discern checked from non-checked exceptions.

b) You are absolutely right.

Thank you for flying JavaRanch.


Yours,
Bu.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot BU
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic