File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Catching exceptions

 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I didn't get why this code compiles:


And this one not:


[ July 26, 2005: Message edited by: George Bolyuba ]
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the compilation error
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try:

In your first example you are catching all Exceptions. Some of those exceptions could be unchecked RuntimeExceptions. Runtime exceptions can be thrown from the JVM and apparently "appear from nowhere". Rather unlikely, but theoretically possible. So the compiler has to allow for the code to compile.


In your second example you are catching all checked IOExceptions and the compiler can see that there are none being thrown or declared to be thrown by the code in the try. So the compiler can rightly refuse to compile the code.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Barry. It sounds reasonably.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry!
Hope I am not misunderstanding you. Do you mean... compiler is intelligent enough to check "if there is a catch, there should be some reason/code which can throw it." ???
And if it doesnt find any such reason/code it will simply say "You have a catch for no reason...i dont find it useful!!!" :roll:
 
amit taneja
Ranch Hand
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes compiler is intelligent because its made by intelligent ppl
 
Satish SN
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi george,

I feel in first code u are catching the Exception class which means it could throw any of the exceptions so compiler need not worry about what exception it is.

in the second case u r specifically mentioning about IOException so u r narrowing down to the specific type of exception so the compiler intelligently looks for the code which for example throws IOException it doesnt find any so it throws compiler error.
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add what Kumar satish said above:

second case:
Code should perform an IO operation with in try block which might throw an IOException, but not neccessarily using the keword throws.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic