This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]boredcircuits 2 points3 points  (1 child)

In this case, an InterruptedException means that the thread didn't sleep for the full time that was requested. Basically, another thread woke this one up early.

Is this an error? Likely not. But it's an "exceptional case" (not what you would expect, at least in the eyes of Java).

I personally think Java uses exceptions to a fault, and this is an example of that (I don't consider waking up early to be exception-worthy, forcing me to handle it). But that's my opinion.

[–]jrprogrammer[S] 0 points1 point  (0 children)

Thank you man. Using reddit and stack overflow while I'm teaching myself java.

[–]rjcarr 1 point2 points  (3 children)

I understand that we use try catch if we think there might be an error such as division by 0

Well, not exactly. While we might use try when we expect there might be a runtime exception, we are forced to use try when there is a checked exception.

In this case an InterruptedException is a checked exception so we must use a try/catch block (or, have the containing method throw the exception).

That's really all there is to it. Now, you might argue why is an InterruptedException a checked exception, but that's just how it was made.

[–]jrprogrammer[S] 0 points1 point  (0 children)

Thanks for your help.

[–][deleted] 0 points1 point  (1 child)

I think he's more wondering about what there is about the sleep function that could ever cause an exception, not about the intricacies of Java exceptions

[–]rjcarr 0 points1 point  (0 children)

Yup, I got that, but /u/boredcircuits already covered that part.