1

I'm trying to figure out when throws might be required in a method signature. I'm a bit confused about this. I've seen cases where throws is not mentioned, where there is just an if (something goes wrong) { throw new someException()} without any throws in the signature.

And then a small follow up question, how would you go about passing an exception to a different class or method? Say you don't want to deal with an exception in the class or method you're currently writing, how would you pass it along?

Thanks in advance!!

Tom
  • 279
  • 1
  • 11
  • 1
    You have to use `throws` if the thing you are throwing is a checked exception (a subclass of `Throwable`, but not a subclass of `Error` or `RuntimeException`), and you don't catch it inside the method body. – Andy Turner Oct 07 '19 at 16:28
  • Correct me if I'm wrong, but aren't you only using throws when you want to tell the caller that it should deal with the exception because the called method is not dealing with it? @AndyTurner – Tom Oct 07 '19 at 16:29
  • 1
    See also: https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html, https://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html – Andy Turner Oct 07 '19 at 16:29

0 Answers0