• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread exceptions

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it that Interrupted exception is a checked exception whereas the IllegalThreadStateException is a runtime exception.

I feel both should have been runtime exception ??
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IllegalThreadStateException is only thrown because of programming errors - the programmer forgot to put the call in a synchronized block / method for the object that wait, notify etc is called on. InterruptedException is, like IOException and SQLException, caused by a situation the programmer can do little about. They cannot be prevented by adding checks etc.

The general convention is:
- unchecked exceptions can be prevented by adding checks. NullPointerException, IllegalArgumentException, NoSuchElementException, in 99.99% of the cases these can be prevented (e.g. by checking against null etc).
- checked exceptions are caused by an external influence. This could be the hard disk, network, but also user interaction in the case of interrupting.

There are of course cases where exceptions don't follow this convention (CloneNotSupportedException is one of these...) but you'll see that most exceptions follow this convention.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic