"Java plane question" How does the Java language handle exception, keyword: throws,throw,try,catch,finally what meaning? Can I throw an exception in a try block?

Source: Internet
Author: User

Throws is a Get exception
Throw throws an exception
A try is a statement that will cause an exception to be handled,
Catch is that if there is an exception, it executes the statement inside him,
Finally, the statement executes regardless of whether or not an exception occurs.

the detailed differences between throw and throws are as follows:

Throw is a statement that throws an exception.
Syntax: Throw (Exception object);
Throw e;

throws is a declaration in which the method may throw an exception . (used when declaring a method, indicating that the method might throw an exception)
Syntax: [(modifier)] (return value type) (method name) ([parameter list]) [Throws (Exception Class)]{...}
public void DoA (int a) throws exception1,exception3{...}

1. Differences

Throws is used to declare all exception information that a method may throw, while throw refers to a specific exception type thrown. In addition throws is the exception declaration but not processing, but the exception to upload, who calls me to whom to deal with.

2. Introduce respectively

Throws: Used to declare an exception, for example, if a method does not want to have any exception handling, then in the absence of any code for exception handling, the method must be declared all possible exceptions (in fact, do not want to do their own work, then to others, tell others I will appear what abnormal, Report your own mistakes and let others handle them.

The format is: Method name (parameter) throws Exception Class 1, Exception Class 2, .....


Throws E1,e2,e3 just tells the program that this method may throw these exceptions, and the caller of the method may want to handle these exceptions, and these exceptions e1,e2,e3 may be generated by the body of the function.
The throw is a clear place to throw this exception.

such as: void DoA (int a) throws exception1,exception3{           try{...           } catch (Exception1 e) {              throw e;           } catch (Exception2 e) {              System.out.println ("Error! ");           }           if (a!=b)              throw new  Exception3 ("Custom Exception");

3 exceptions can be generated in a code block (Exception1,exception2,exception3).
If a Exception1 exception is generated, it is then thrown after the capture and processed by the caller of the method.
If a Exception2 exception is generated, the method processes itself (that is, SYSTEM.OUT.PRINTLN ("Error!"). ");)。 So the method will not throw out the Exception2 exception, void DoA () throws Exception1,exception3 inside the Exception2 also do not have to write.
The Exception3 exception is a logic error in the method, the programmer has done their own processing, in the case of logic error thrown exception Exception3, the method's caller will also handle this exception.

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.
The throws statement is used after the method declaration, which means that the exception is thrown and handled by the caller of the method.

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 the word, that is, you turn that tendency into reality.

1, throws appears in the method function head, and throw appears in the function body.
2. Throws indicates that there is a possibility of an exception, which does not necessarily occur; throw throws an exception, and the throw throws a certain exception.
3, both are negative handling of the abnormal way (the negative here is not to say this way bad), just throw or may throw an exception, but not by the function to deal with the exception, the real handling of the exception by the function of the upper call processing.

Java face question How does the Java language handle exception, keyword: What does throws,throw,try,catch,finally mean? Can I throw an exception in a try block?

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.