Error
During the running of the program, some undesirable effects may occur, which will certainly prevent our program from being executed according to the instructions. This unexpected effect must be thrown to tell us that there is a well-defined rule Throwable in Java, which is the parent class of all exceptions and errors.
Once exceptions and errors occur and are not handled accordingly, the program will be interrupted.
Error
Usually there are some physical problems. The program instructions cannot handle the problems that occur in the JVM virtual machine itself.
Common errors are ::
StackOverflowError
Stack memory overflow
OutOfMemoryError
Heap memory overflow
Exception
It is usually an abnormal phenomenon stipulated by humans, and it is usually that the given program instructions produce some things that do not meet the specifications.
Exceptions can be divided into:
compile-time exceptions and runtime exceptions .
Runtime exception
Usually we think that Error and RuntimeException are counted as runtime exceptions.
Run-time exceptions are also called unchecked exceptions. They are not prompted and discovered when javac is compiled. They are not required to be processed when the program is written. If we want to, we can add processing means (try-catch / throws)
Common runtime exceptions:
InputMisMatchException
Input does not match
int value = input.nextInt (); // abcNumberFormatException
Number formatting exception
int value = Integer.parseInt ("123.45");NegativeArraySizeException
Negative array length
int [] array = new int [-2];ArrayIndexOutOfBoundsException
Array index out of bounds
int [] array = {1,2,3};
array [5];NullPointerException
Null pointer exception
int [] [] array = new int [3] [];
array [0] [0] = 10;
Person p = null;
p.getName ();ArithmeticException
Number exception
10/0 Integers are not allowed to be divided by 0 Infinity decimal divided by 0 will produce infinityClassCastException
Abnormal styling
Person p = new Teacher ();
Student s = (Student) p;StringIndexOutOfBoundsException
Bounds string
String STR = "ABC";
str.charAt (. 5);IndexOutOfBoundsException
集合越界
List家族
ArrayList list = new ArrayList();
list.add(); list.add(); list.add();
list.get(5);IllegalArgumentException
Illegal parameter exception
ArrayList list = new ArrayList (-1);
Compile time exception
Compile-time exceptions refer to exceptions other than Error and RuntimeException, also called check exceptions.
When compiling javac, it is mandatory that we must deal with such exceptions (try-catch or throws), because such exceptions are very likely to cause problems during the process of running the program.
Compile-time exceptions are a very large branch of the system in the Exception family, as long as they are not runtime exceptions, they are all compile-time exceptions.
Examples:InterruptException
try{
Thread.sleep(5000);
}catch(InterruptException e){
}
Two methods of exception handling
try{}catch(){}[ finally{} ]
- try cannot appear alone
- Catch or finally must be added later
- catch has a set of brackets (NullPointerException) The purpose is to catch a certain type of exception
- There can be many catch
- There is no inheritance relationship between the caught exceptions
- The captured exception needs to be captured from small to large
- Finally, it does not have to exist.
If there is finally structure, it must be executed.
To extend a small question: the
difference between final and finalize?
-
Final is a feature modifier to modify variable attribute method class
Modified variable: If the modified basic type, the value cannot be changed. If the reference type is modified, the address cannot be changed (if the variable does not have an initial value, assign it a chance).
Modified attribute: the characteristics are similar to the modified variable (requires that the attribute must be assigned an initial value or compilation error)
modified method: cannot be
modified by subclass Class: cannot be inherited by other subclasses -
Finally is part of the means to deal with exceptions
try {} catch () {} The part after
this is optional. If there is only one part, it must contain one and must be executed. -
finalize is a protected method in the Object class when
there is no reference to the object-it will be recycled by GC.
When the object is recycled, the finalize method is called by default.
If you want to see the effect of object recycling, you can override public void finalize () }
There may be minor problems
in handling exceptions inside the method. If the return value is included in the method,
no matter where the return value of the return keyword is, it will eventually be executed
. The specific result of the return value depends on the situation (whether there is an exception)
throws
- Exceptions can only be thrown on methods. Attributes cannot be thrown
- Both methods and constructors can throw exceptions, not blocks
- Methods can throw more than one exception separated by commas
- The exception thrown is similar to multiple catches, either it doesn't matter (parallel), or a small exception is thrown first, then a large exception is thrown
Custom exception
Custom runtime exception:
inherit RuntimeException class
public class MyRuntimeException extends RuntimeException{
}
Custom compile-time exception:
inherit Exception class
public class MyException extends Exception{
}
You can also add the function of exception information description to the custom exception:
public class MyRuntimeException extends RuntimeException{
public MyRuntimeException(){}
public MyRuntimeException(String msg){
super(msg);
}
}