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 is explained