Explain Thread exceptions?


Sleep method is enclosed in a try blocked followed by a catch block.

This is necessary because the sleep( ) throws an exception, which should be caught .if we fail to catch the exception ,program will not compile.

Java run system will throw IllegalThreadStateException whenever we attempt to involve a method that a thread cannot handle in the given state.

For ex:  a sleeping thread cannot deal with the resume( ) method because a sleeping thread can not receive any instructions. The same is true with the suspend() method when it is used on a blocked thread.

When ever we call a thread method that is likely to throw an exception. we have to supply an appropriate exception handle to catch it.

The catch statement may take one of the following forms.

catch(ThreadDeath e)
{
  ………………   //killed thread
}

catch(InterruptedException e)
{
…………..    //cannot handle it in the current state
}

catch(IllegalArgumentException e)
{
……………………………..   //illegal method argument
}

catch(Exception e)
{
………………….  //any other
}

Related

Java 2044192111255801781

Post a Comment

emo-but-icon

item