What happens if a method throws an exception that was not specified in the method declaration with “throws”

前端 未结 6 1295
忘掉有多难
忘掉有多难 2021-01-01 11:27

I\'ve never used the \"throws\" clause, and today a mate told me that I had to specify in the method declaration which exceptions the method may throw. However, I\'ve been u

6条回答
  •  囚心锁ツ
    2021-01-01 12:07

    The throws key word is used to throw an exception to another method.

    It eases the handle exception to the user. Because then all of the exceptions can be handled in a method which is used to run.

    Mostly it is mainly a method, so that the user does not need to explore inside the method.

    It also throws keyword force to the compiler to handle the exception which could be occurring.

    If you were a API developer, when you write a code, you might see that an exception could occur, so you use the throws keyword to handle it when the method runs.

提交回复
热议问题