-1

For an example if you use something like this:

Thread.sleep(400)

The compiler throws an error: Main.java:10: error: unreported exception InterruptedException; must be caught or declared to be thrown

But if you handle it:

try {
  Thread.sleep(400)
} cacth(InterruptedException error) {
  // Code here
}

Then it won't. Why?

Ole V.V.
  • 76,217
  • 14
  • 120
  • 142
  • Also see [this](https://stackoverflow.com/questions/3755203/why-must-only-certain-exceptions-be-declared-as-thrown-in-method-signature) – Federico klez Culloca May 22 '22 at 10:38
  • 3
    Because the language was designed like this. Which in turn it was in order to help you not forget to handle the potential exception. – Ole V.V. May 22 '22 at 11:10
  • Usage: an *exception* is *thrown* on runtime. In this case an `InterruptedException` in case the current tread gets interrupted while sleeping. The compiler prints or lists or reports errors, it does not throw them. – Ole V.V. May 22 '22 at 11:13

0 Answers0