Directory
Java Handling Exception mode
The role of Throw
The role of throws
Method principle
Example
Summarize
Personal instances
1.java Handling Exception Mode
In Java code, if an exception occurs, the JVM throws an exception object, causing the program code to break, which is what the JVM is doing: Creating the exception object and then throwing it, for example:
int i= 1; int j = 0; int res = 0; res = i/j;//except 0 error System.out.println (res);
These 5 lines of code run to the fourth sentence will break because the JVM throws an exception
The role of 2.throw
Throw exceptions manually but sometimes some bugs don't look like bugs to the JVM, for example
int age = 0; age = -100; System.out.println (age);
Very normal variable assignment, but in our eyes it seems not normal, whose age will be negative. So we need to manually throw an exception, which is the role of throw.
int num1=10;
int num2=0;
int rest=num1/num2;
if (num2=0) {
throw new ArithmeticException ("Age cannot be negative!") ");//Create exception object, throw exception. ArithmeticException is a divisor of 0 and an exception occurred
}
System.out.println (age);
The role of 3.throws
Declaring the method may be avoided exception is thrown, it is necessary to do processing, so there is try-catch in Java but sometimes a method produces an exception, but do not know how to handle it, then put it regardless, when there is an exception thrown when the method will be broken, and the exception is thrown into the method of the caller. This is a bit like the subordinates can not handle the problem of the boss in the hands of the same, this situation is called avoidance exception but this makes it dangerous to call this method, because no one knows when this method will throw a kind of exception to the caller, so when defining the method, You need to use throws in the method header to declare exceptions that this method might avoid
void Fun () throws Ioexception,sqlexception { ... }
This means that the fun method may throw two exceptions, so when you call fun, you're ready to do it, like this.
Try {fun (); } catch (IOException e) { }catch (SQLException e) { }
4. Method Principle
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.
The exception that the statement throws is a custom exception for the user program and an application-specific exception that must be defined by means of the throws and throw statements.
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 . Syntax: [(modifier)] (return value type) (method name) ([argument list]) [Throws (Exception Class)]{...} When declaring a method, indicating that the method might throw an exception) public void DoA (int a) throws exception1,exception3{...}
5. For 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.
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 ("Something went wrong!" ");)。 So the method will not throw out the Exception2 exception, void DoA () throws Exception1,exception3 inside the Exception2 also do not have to write. And the Exception3 anomaly is the method's
There is a logic error and the programmer has handled it himself, and the caller of the method will handle the exception if it throws an exception Exception3 in the case of the logic error.
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.
6. Summary
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.
7. Personal instances
Java Error result Throw/throws