• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why catch block is not executing after try ?

 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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" ?
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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() .
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic