The difference between throws and throw in Java

Source: Internet
Author: User
Tags getmessage

Java

The throws and throw in Java have been not understood. I have been looking into these two aspects recently, I can understand a little. If my opinion below is wrong, I wish to point out that I will improve it.      Throw: (Approach to object) throws an exception, which can be either system-defined or self-defined. Here are two examples:   throws a system exception in Java:Public class One {Public void Yichang () { numberformatexception e = new NumberFormatException (); throw e; }Public static void Main (string[] args) {One test = new One ();try{Test.yichang ();}catch (NumberFormatException e) {System.out.println (E.getmessage ()); } }}throws a custom exception:public class People {public static int check (String strage) throws myexception{int age = Integer.parseint (strage);if (age < 0) { throw new MyException ("Age cannot be negative!") "); }return age; }Public static void Main (string[] args) {try{int myage = Check (" -101");System.out.println (myage);}catch (NumberFormatException e) {System.out.println ("Data format error");System.out.println ("Reason:" + e.getmessage ());}catch (myexception e) {System.out.println ("Data logic Error");System.out.println ("Reason:" + e.getmessage ()); } }}Public class MyException extends exception{private static final long serialversionuid = 1L;private String name;Public myexception (String name) {this.name = name; }Public String GetMessage () {return this.name; }}throws: (Exception thrown for a method)Throws an exception, which can be either system-defined or self-defined.throws a system exception in Java:public class One {Public void Yichang () throws NumberFormatException{int a = Integer.parseint ("10L"); }Public static void Main (string[] args) {One test = new One ();try{Test.yichang ();}catch (NumberFormatException e) {System.out.println (E.getmessage ()); } }}throw a custom exception:public class People {public static int check (String strage) throws MyException{int age = Integer.parseint (strage);if (age < 0) {throw new MyException ("Age cannot be negative!") "); }return age; }Public static void Main (string[] args) {try{int myage = Check (" -101");System.out.println (myage);}catch (NumberFormatException e) {System.out.println ("Data format error");System.out.println ("Reason:" + e.getmessage ());}catch (myexception e) {System.out.println ("Data logic Error");System.out.println ("Reason:" + e.getmessage ()); } }} public class MyException extends exception{private static final long serialversionuid = 1L;private String name;Public myexception (String name) {this.name = name; }Public String GetMessage () {return this.name; }}So here's what I'm going to say: if it's a system anomaly, you don't have to do anything ., you can also throw an exception for the method, becausefor system exceptions, they can be automatically captured by the system., so whether this anomaly is to be resolved within the method or given to the upper function to solve the effect is the same.but I looked at a lot of information, even if the exception can be caught by the system, or it is recommended to write a throws to the method, because this can be done in a large task, the other programmer will know what happens here.  If the exception is defined by itself, you must use throws to throw an exception that the method might throw, or the compilation will error. Of course, you need to understand that exceptions are seen in Java Chinese as an object.
And all system-defined compile and run exceptions can be automatically thrown by the system, known as standard exceptions, but in general Java strongly requires the application to perform full exception handling, user-friendly hints, or corrections to keep the program running.
Go straight to the chase:
1. User program custom exceptions and application-specific exceptions, you must use the throws and throw statements to define the throw exception.

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

1.2 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 ioexception,{
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 throws and throw 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.