catalogue
1. Essence of abnormal mechanism
1. Essence of abnormal mechanism
- It is the mechanism that the program exits safely when there is an error in the program. Java uses an object-oriented way to handle exceptions
- Throw exception: when executing a method, if an exception occurs, the method generates an object representing the exception, stops the current execution path, and submits the exception object to JRE
- Catch exception: after getting the exception, JRE looks for it in the call stack of the method, and starts backtracking from the method generating the exception until it finds the corresponding exception handling code
- Error: an error that cannot be handled by the program, which indicates a more serious problem in running the application and cannot be handled by general programming operations
- Exception: exceptions that can be handled by the program itself, such as null pointer exception, array index out of bounds exception, type conversion exception, arithmetic exception, etc
- RuntimeException: such as division by 0, array subscript out of bounds, null pointer, etc. it is usually caused by programming errors
- Checked Exception: the general name of all exceptions that are not runtimeexceptions, also known as "checked exceptions", such as IOException, SQLException and user-defined Exception. Such exceptions must be handled during compilation, otherwise they cannot be compiled.
2. Exception handling
- Catch exceptions: catch exceptions through three keywords: try catch finally. Try is used to execute a program. If there is an exception, the system throws an exception. You can catch and handle it through its type. Finally, a unified exit is provided for exception handling through the finally statement, and the code specified by finally must be executed (there can be multiple catch statements, and there can be only one finally statement).
- Declare exception: in some cases, the current method does not need to handle the exception, but passes it up to the method calling it for processing. Or when CheckedException occurs, it may not be handled immediately. You can throw the exception again.
- If an exception may occur in a method, but it is uncertain how to handle it, the exception that the method may throw should be declared at the head of the method according to the exception specification. If a method throws multiple checked exceptions, all exceptions must be listed in the header of the method, separated by commas.
- Custom Exception: in the program, you may encounter the problem that any standard Exception class provided by JDK cannot fully describe the problem we want to express. In this case, you can create your own Exception class, that is, custom Exception class. The custom Exception class only needs to derive a subclass from the Exception class or its subclass.
- If the custom Exception class inherits the Exception class, it is an checked Exception and must be handled; If you don't want to handle it, you can let the custom Exception class inherit the runtime Exception RuntimeException class.
- Traditionally, a custom exception class should contain two constructors: one is the default constructor, and the other is the constructor with details:
/**IllegalAgeException: Illegal age Exception, inheriting Exception class*/ class IllegalAgeException extends Exception { //default constructor public IllegalAgeException() { } //Constructor with detailed information, which is stored in message public IllegalAgeException(String message) { super(message); } }
3. Array declaration
//Create a one-dimensional array of basic types public class Test { public static void main(String args[]) { int[] s = null; // Declaration array; s = new int[10]; // Allocate space to the array; for (int i = 0; i < 10; i++) { s[i] = 2 * i + 1;//Assign values to array elements; } } }
//Create a one-dimensional array of reference types class Man{ private int age; private int id; public Man(int id,int age) { super(); this.age = age; this.id = id; } } public class Test { public static void main(String[] args) { Man[] mans; //Declare an array of reference types; mans = new Man[10]; //Allocate space to the reference type array; Man m1 = new Man(1,11); Man m2 = new Man(2,22); mans[0]=m1;//Assign a value to the element of the reference type array; mans[1]=m2;//Assign a value to the element of the reference type array; } }
- The array is not really created when it is declared. The JVM allocates space only when the array object is instantiated
4. Initialization of array
- Static initialization: allocate space and assign values to array elements while defining the array
int[] a = { 1, 2, 3 };// Static initialization of basic type array; Man[] mans = { new Man(1, 1), new Man(2, 2) };// Statically initialize the reference type array;
- Dynamic initialization: array definition is separated from the operation of allocating space and assigning values to array elements
int[] a1 = new int[2];//Dynamically initialize the array and allocate space first; a1[0]=1;//Assign values to array elements; a1[1]=2;//Assign values to array elements;
- Default initialization: once the array is allocated space, the elements in it are implicitly initialized in the same way as the instance variable
int a2[] = new int[2]; // Default: 0,0 boolean[] b = new boolean[2]; // Default value: false,false String[] s = new String[2]; // Default: null, null
5. Traversal of array
- The array can also be traversed by using a general loop, but jdk1 5. A new for each loop is added for array traversal:
public class Test { public static void main(String[] args) { String[] ss = { "aa", "bbb", "ccc", "ddd" }; for (String temp : ss) { System.out.println(temp); } } }
- However, the elements cannot be modified when traversing the array using this method
6. Copy of array
- The System class contains the static void array copy (object src, int srcpos, object dest, int destpos, int length) method, which can assign the elements in the src array to the elements in the dest array
- srcpos: Specifies the number of elements in the src array from which the assignment starts
- length: specifies how many elements of src array are assigned to the elements of dest array
public class Test { public static void main(String args[]) { String[] s = {"A","B","C","D","E"}; String[] sBak = new String[6]; System.arraycopy(s,0,sBak,0,s.length);//Copy of array } }