• 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

Why catch block is not executing after try ?

 
Greenhorn
Posts: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class ThreadSleep extends Thread
{
public void run()
{
System.out.println("begining run");
for(int i=0;i<=5;i++)
{
System.out.println("i="+i);
try{
Thread.sleep(1000); //generating Checked Exception: InterruptedException
}
catch(InterruptedException ie){
System.out.println("handled successfully ");
}
}
System.out.println("end of run");
}
public static void main(String s[])
{
ThreadSleep ts=new ThreadSleep();
System.out.println("begining main");
ts.start();
for(int j=0;j<=5;j++)
{
System.out.println("j="+j);
}
System.out.println("end main");
}
}

After executing,the prog is successfully executed. But i found that the catch block is not executing.
Why is that, is the catch block not able to handle the exception ? the outputis as follows:
begining main
j=0
j=1
j=2
j=3
j=4
j=5
end main
begining run
i=0
i=1
i=2
i=3
i=4
i=5
end of run

where is the statement: "handled successfully" ?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will only handle an exception if one occurs. If none does, then there is nothing to handle. Simple as that.
 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:It will only handle an exception if one occurs. If none does, then there is nothing to handle. Simple as that.



But according my point of view: as sleep() called,the current run() execution is blocked for particular time and JVM starts executing main(). So here the InterruptedException is generating.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please indent our code and use code tags; it is very difficult to read as it is.
An InterruptedException will be thrown from a Thread as it goes into sleep if it has been interrupted by another thread. I can see nowhere that another thread is interrupting it. Read about exceptions in the Java Tutorials. Remember a throws clause means a method might throw an exception, not that it will throw that exception.
 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please indent our code and use code tags; it is very difficult to read as it is.
An InterruptedException will be thrown from a Thread as it goes into sleep if it has been interrupted by another thread. I can see nowhere that another thread is interrupting it. Read about exceptions in the Java Tutorials. Remember a throws clause means a method might throw an exception, not that it will throw that exception.



Then main() is also treated as a thread by JVM . at tihs point of time main() is simultaneously executing with the run() .
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Soumya Padhiary wrote:

Campbell Ritchie wrote:Please indent our code and use code tags; it is very difficult to read as it is.
An InterruptedException will be thrown from a Thread as it goes into sleep if it has been interrupted by another thread. I can see nowhere that another thread is interrupting it. Read about exceptions in the Java Tutorials. Remember a throws clause means a method might throw an exception, not that it will throw that exception.



Then main() is also treated as a thread by JVM . at tihs point of time main() is simultaneously executing with the run() .



Which has nothing to do with InterruptedException. Read the docs for that class, or read what any tutorial has to say about it. It's not thrown just because a context switch occurs, and it's not thrown when you wake up normally from a sleep() call because the sleep time has expired.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic