• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the output of the program?please explain....

 
Krishna prasad ambala
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the output of the program? if the run() method executes and enters into try block will it throw any exception......if not.explain why.


i have an idea that sleep() will throw InterruptedException..then why it is not printing inturruptedexception.


 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you try to execute it, What it shows when it runs !

if the run() method executes and enters into try block will it throw any exception......if not.explain why.


The exception will be thrown only if there is any runtime anomalies occurred !

i have an idea that sleep() will throw InterruptedException..then why it is not printing inturruptedexception.


Yes, the sleep() method throws InterruptedException , but that dosent mean it throws that exception , whenever we call it ! Read the docs for more explanation when it throws Exception !
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Krishna prasad ambala:
i have an idea that sleep() will throw InterruptedException..then why it is not printing inturruptedexception.


Hi remove try and catch and check

it will print(program is fine ..then why you expect Exception )
-------------
started
ran
---------------
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Thread#sleep() method doesn't "throw" a checked Exception, it "declares" a checked Exception, telling the compiler it "might throw" an Exception. That is what the throws keyword does.

If you take the try-catch away from the Thread.sleep() call, your class won't compile.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic