The difference between throw and throws in Java

Source: Internet
Author: User
Tags throw exception

The difference between throw and throws in Java

Exceptions that are automatically thrown by the system
All system-defined compile and run exceptions can be automatically thrown by the system, known as standard exceptions , and Java strongly requires the application to perform full exception handling, give user-friendly hints, or fix the program to continue.

exception thrown by statement
User program custom exceptions and application-specific exceptions, you must use the throws and throw statements to define the throw exception.


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{...}

Example:

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 ("Something went wrong!" ");
}
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.

While
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.

The difference between throw and throws in Java

Related Article

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.