aspose file tools*
The moose likes Java in General and the fly likes Why catch block is not executing after try ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why catch block is not executing after try ?" Watch "Why catch block is not executing after try ?" New topic
Author

Why catch block is not executing after try ?

Soumya Padhiary
Greenhorn

Joined: Jan 10, 2013
Posts: 20

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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Soumya Padhiary
Greenhorn

Joined: Jan 10, 2013
Posts: 20

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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Jan 10, 2013
Posts: 20

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why catch block is not executing after try ?
 
Similar Threads
exception handling
confused about the output of the program on exceptions?
shift operators
Enthuware questions
About Exception Handling