• 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 Handling

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

I have a doubt in Exception Handling.

We can handle exception in two ways:

1)Enclosing code in try..catch block.
2)using throws clause in the caller method.
Example: public void run() throws IOException{

InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int number1=br.readLine(); //This is the method which will throw an
//IOException.So "Run"method handles this
//exception using 'throws' clause(second
//way).

My question is "who is going to handle this exception?", when run() method doesn't wrap the code in try...catch block, it simply throws the exception again.

I need assistance...

thanks
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means anybody who calls your run() method is in the same boat you were. They have to either catch the exception you declare or declare that they can throw it, too.

You used run() as an example, which makes me think of Runnable classes. If you're not using Threads ignore this ... Can Runnable.run() declare a checked exception? Who would be calling run()? What could they possibly do with the exception?
 
reply
    Bookmark Topic Watch Topic
  • New Topic