MODULE 7 Exceptions
----------------------------
Unexpected situations encountered during normal execution of the program
Factors that cause an exception:
1) intrinsic factors of the program itself
2) external factors, the procedure does not need to pay attention to
Example: array access out of bounds exception; divided by 0;
Exception occurs:
1) throws an exception at the place where it occurred
2) The normal process of the program will change
3) capture is required for thrown exceptions
try {
Normal program execution process
}catch (Exception 1) {
Handling code for Exception 1
}catch (Exception 2) {
Handling code for Exception 2
}finally{
Whether or not an exception occurs, you have to perform the
}
Attention:
When multiple exceptions are caught and there is a parent-child relationship between these exceptions, be sure to catch the child exception before catching the parent exception (because the catch of the exception is sequential)
try{
//
}catch (IOException e)) {//Small exception
Deal with IOException
}catch (Exception e) {//Large exception
Deal Wwith Exception
}
Exercise: Divtest.java
Method: div (int i,int j) {}//implement i/j and print output results
Main: Call the div () method, test
Exception propagation Stack
-------------------------
1) The exception is thrown to its upper-level caller at the point where it occurs
2) at any point on the thrown propagation path, the exception can be caught on the handle
3) Once an exception is captured, its upper-level caller is no longer affected, but if not processed, it will eventually be thrown to the JVM, causing the program to terminate unexpectedly
Common methods of Exceptions:
1) getMessage () gets a simple description of the exception, and returns a string
2) Printstacktrace () print out an abnormal and complete propagation path
Exercise: Divtest1.java
Main--DIV1 ()-Div2 ()-div3 ()-DIV ()
Common exceptions
1.java.lang.arithmeticexception arithmetic exception divided by 0 o'clock
2.NullPointerException NULL pointer exception
Student s;
S.display ();
Operate on an empty object
3.ArrayIndexoutofBoundsException
The architecture of the exception
--------------------
Object--Throwable--
1.Error
Ignoring the error of the program itself, it is caused by external factors, and the program terminates directly. (such as system crashes, low memory, etc.)
2.Exception
All exception classes in Java inherit from that class, and those exceptions that the program can handle
1) RuntimeException is also called unchecked exception
Exceptions that are not self-recoverable by the program, are handled with try/catch, are often caused by logic errors in the program, and should be avoided as much as possible.
2) non-runtimeexception also known as checked exception
Exceptions that must be Try/catch handled in the program
Exceptions inherited from RuntimeException are unchecked
Exceptions inherited from exceptions other than exception or runtimeexception are checked
Practice:
Custom exception classes, account bank accounts deposit/withdrawal,
Original:
When the amount is illegal, return 0;
Modified by:
Prompt user for illegal amount and inform account balance and operation amount
Assertion Assertion
----------------------------
Through simple parameter setting, the debug statement in the control program is effective or invalid.
Application:
The code that is used in the program for temporary debugging, you want the system to control when the code is released, and when debugging is code effective
Keywords: assert
Usage:
An assert Boolean expression;
Assert Boolean expression: string
To judge a Boolean expression:
When the result is true, the program continues to execute down
When the result is false, the Java.lang.AssertionError exception is thrown, and the hint message is the string following the Boolean expression
The runtime passes assertions in the parameter control program:
Java-ea Assertiontest
Core Java 7 exception