Java throws an exception in the usage of thorw and throws, and throws thorwthrows

Source: Internet
Author: User
Tags java throws

Java throws an exception in the usage of thorw and throws, and throws thorwthrows

Java exceptions are treated as an object.

All system-defined compilation and running exceptions are automatically thrown by the system and become standard exceptions.

However, in general, java strongly requires the application to complete Exception Handling, give user-friendly prompts, or correct the code to continue running.

1. User-defined exceptions and application-specific exceptions must be defined with throw and throws statements to throw exceptions.

1.1throw is an exception thrown by the statement;

Syntax: throw (exception object); eg: throw e;

Location: In the method body, an exception is thrown and is processed by the statements in the method body.

1.2throws is a declaration that may throw an exception. Indicates that the method may throw an exception or not.

Syntax: throws (exception type ){.......}

Eg: public void do (int a) throws Exception 1, Exception 2 {......}

Location: used after the method declaration to handle an exception thrown by the caller of the method.

Feature: both of them are passive Exception Handling Methods (not this method is not bad), but they simply throw or may throw an exception, but no function will handle the exception, real Exception Handling involves upper-layer function call processing.

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.