Java Exception Class Hierarchy Diagram In Java, all exceptions have a common ancestor, the Throwable class in the java.lang. Throwable: has two weights
Important subclasses: Exception (exception) and Error (error) , both of which are important subclasses of Java exception handling, each containing a large
Quantum class.
Error (Error) : It is an error that the program cannot handle , indicating a serious problem in running the application. Most errors are related to the code writer
The operation of the line is irrelevant, but indicates a problem with the JVM ( Java Virtual Machine) when the code is running. For example, the Java virtual machine runs incorrectly
( Virtual MachineError ), which occurs when the JVM no longer has the memory resources needed to continue the operation
OutOfMemoryError . When these exceptions occur, the Java Virtual Machine ( JVM ) generally chooses the thread to terminate.
These errors indicate that the failure occurred in the virtual machine itself, or when the virtual machine tried to execute the application, such as the Java virtual machine running error
( Virtual MachineError ), class definition errors ( NoClassDefFoundError ), etc. These errors are uncheckable because it
They are outside the control and processing power of the application program, and most of them are not allowed to occur when the program is running. reasonable for the design
As far as the application is concerned, even if an error does occur, it should not inherently try to handle the abnormal condition it causes. In Java , wrong
Mistakenly described by the subclass of Error .
Exception (Exception) : It is an exception that the program itself can handle . The Exceptionclass has an important subclass
RuntimeException . RuntimeExceptionby the Javavirtual machine.
NullPointerException (this exception is thrown when the variable to be accessed does not refer to any object),
ArithmeticException (arithmetic operation exception,which is thrown when 0
ArrayIndexOutOfBoundsException (subscript out of bounds exception).
Note: The difference between exceptions and errors: exceptions can be handled by the program itself, but errors cannot be handled.
Common methods of Throwable class
• public string getMessage() : returns detailed information when an exception occurs
• public string toString() : returns a brief description of when an exception occurred
• public string getLocalizedMessage() : returns the localized message of the exception object. Override with a subclass of Throwable
This method, can claim localization information. If the subclass does not override this method, the information returned by this method is the same as getMessage ()
returns the same result as
• public void printStackTrace() : Print the exception information encapsulated by the Throwable object on the console . Summary of exception handling
• try block: used to catch exceptions. It can be followed by zero or more catch blocks, if there is no catch
block, it must be followed by a finally block.
• catch block: used to handle the exception caught by try .
• finally block: Regardless of whether the exception is caught or handled, the statements in the finally block will be executed.
When a return statement is encountered in a try block or a catch block , the finally statement block will be executed before the method returns. In the following 4 special
case, the finally block will not be executed:
1. An exception occurred in the finally statement block .
2. In the previous code, System.exit() was used to exit the program.
3. The thread where the program is located dies.
4. Turn off the CPU .
What to do if some fields do not want to be serialized in Java serialization
For variables that do not want to be serialized, use the transient keyword to modify.
The role of the transient keyword is to prevent serialization of variables modified with this keyword in the instance; when the object is deserialized, it is
Variable values modified by transient will not be persisted and restored. transient can only modify variables, not classes and methods.
Get two methods commonly used for keyboard input
Method 1 : Via Scanner
Scanner input = new Scanner(System.in);
String s = input.nextLine(); input.close();
Method 2 : via BufferedReader
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String s = input.readLine();
Interface Inheritance and Implementation
The collection class is stored in the Java.util package, and there are three main types: set ( set), list ( list contains Queue ) and map ( map ) .
1. Collection : Collection is the most basic interface for collections List , Set , and Queue .
2. Iterator : Iterator, you can traverse the data in the collection through the iterator
3. Map : is the basic interface List of the mapping table
Java 's List is a very commonly used data type. List is an ordered Collection . There are three implementation classes in Java List :
ArrayList、Vector 和 LinkedList。
ArrayList (array)
ArrayList is the most commonly used List implementation class. It is implemented internally through an array, which allows fast random access to elements. lack of array
The point is that there can be no interval between each element. When the size of the array is not enough, it is necessary to increase the storage capacity, and the data that already exists in the array must be copied.
to the new storage space. When inserting or deleting elements from the middle of the ArrayList , the array needs to be copied, moved, and replaced.
The price is relatively high. Therefore, it is suitable for random lookup and traversal, not for insertion and deletion.
Vector (array implementation, thread synchronization)
Like ArrayList , Vector is also implemented through arrays, the difference is that it supports thread synchronization, that is, only one
A thread can write Vector to avoid inconsistency caused by simultaneous writing by multiple threads, but synchronization requires a high cost. Therefore, access
It is slower than accessing ArrayList .
LinkList (linked list)
LinkedList stores data in a linked list structure, which is very suitable for dynamic insertion and deletion of data, and the speed of random access and traversal is relatively slow. Other
In addition, it also provides methods not defined in the List interface, which are specially used to operate the header and tail elements, which can be used as stacks, queues, and double
Used by the queue.