• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic