The difference between throw and throws

Source: Internet
Author: User

The throws statement is used after the method declaration, which means that the exception is thrown and handled by the caller of the method.

The throw statement is used in the body of the method to indicate that an exception is thrown and processed by the statement in the method body.


Throws primarily declares that this method throws an exception of this type so that its caller knows to catch the exception.

Throw is a specific action that throws an exception, so it is throwing an exception instance.


throws shows that you have that possibility, inclination.

throw, that's how you turn that tendency into reality.


Meanwhile:
1, throws appear in the method function header, and throw appears in the function body.
2, throws, indicates that there is a possibility of an exception, which does not necessarily occur, that throws an exception, and that the throw throws a certain exception.
3, both are negative ways of dealing with exceptions (the negativity here is not to say that this is not a good way), just throw or maybe throw an exception, but not by the function to handle the exception, the real handling exception is handled by the upper call of the function.



The difference between throw and throws

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.