• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Checked Exception Confusion

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchere
I am new to javaranch . I have a confusion related to checked Exception . As in code showm below if we have empty try block and catches ’IOException’ ( as at line 1 ) it will give a compile time error saying
::“exception java.io.IOException is never thrown in body of corresponding try statement”
Where as if we catch ‘Exception’ with an empty try block code compiles fine.
I just want to know both ‘IOException’ & ‘Exception’ are checked exceptions than why code with line 2 compiles fine whereas code with line 1 fails to compiles ………

code ::

import java.io.*;
class ZZ{
public static void main(String args[]){

try{ }catch(IOException e){e.printStackTrace();}//line 1
//try{ }catch(Exception e){e.printStackTrace();}//line 2
}
}




Thanks in advance
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's because even though Exception itself is a checked Exception, it has subclasses which are unchecked (RuntimeException and its subclasses).

IOException and its subclasses are checked exceptions.
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IOExceptions are thrown only if you use io methods such as FileWriter, PrintWriter, BufferedWriter etc. becos there is no code in you try block with that kind of code providing a catch block for it will give a compile error because the compiler can say for sure that IOException will never be thrown becos you are not using any io methods. but when you say just Exception then anytype of exception will be caught because you are not specifying a specific exception. hence anything that can be considered throwable will be caught by just using Exception. hence the empty tags will be ok to use with the Exception.


hope this helps
 
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic