This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Catching exceptions

 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the compilation error
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Barry. It sounds reasonably.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • 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
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic