43. What is different between throw and throws keywords

What is different between throw and throws keywords?

  • Throw is a keyword which is used to explicitly throw an exception in the program inside a method or inside a block code whereas throws keyword is used to with methd signature to declare an exception may be occur in that method. And when we use throws keyword, we notify for the caller method have to handler that exception or transfer that exception down to previous caller
  • Throw keyword follow by an instance of exception class while throws keyword folow by Exception class.
  • You can only throw one exception at a time, but you can declare mutiple exception with throws keywords.
  • Using throw keyword, only unchecked exception can be propagated whereas using throws keywords, both checked and unchecked exception can be propagated.

What is the advantage of throws keywords:

  • It helps the code more clearly and help the developers can handler exception without missing the exception
  •  It supports checked exception propagation and throw don’t support checked exception propagation

Leave a Reply

Your email address will not be published. Required fields are marked *