Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception Handling  RSS feed

 
Sundaralingam Subramaniyan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!