Exceptions and errors in Java

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.
Insert picture description here
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 ::
StackOverflowErrorStack memory overflow
OutOfMemoryErrorHeap 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:

  1. InputMisMatchExceptionInput does not match
    int value = input.nextInt (); // abc
  2. NumberFormatExceptionNumber formatting exception
    int value = Integer.parseInt ("123.45");
  3. NegativeArraySizeExceptionNegative array length
    int [] array = new int [-2];
  4. ArrayIndexOutOfBoundsExceptionArray index out of bounds
    int [] array = {1,2,3};
    array [5];
  5. NullPointerExceptionNull pointer exception
    int [] [] array = new int [3] [];
    array [0] [0] = 10;
    Person p = null;
    p.getName ();
  6. ArithmeticExceptionNumber exception
    10/0 Integers are not allowed to be divided by 0 Infinity decimal divided by 0 will produce infinity
  7. ClassCastExceptionAbnormal styling
    Person p = new Teacher ();
    Student s = (Student) p;
  8. StringIndexOutOfBoundsExceptionBounds string
    String STR = "ABC";
    str.charAt (. 5);
  9. IndexOutOfBoundsException 集合越界
    List家族
    ArrayList list = new ArrayList();
    list.add(); list.add(); list.add();
    list.get(5);
  10. IllegalArgumentExceptionIllegal 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.
Insert picture description here
Examples:InterruptException

try{
	Thread.sleep(5000);
}catch(InterruptException e){

}

Two methods of exception handling

try{}catch(){}[ finally{} ]

  1. try cannot appear alone
  2. Catch or finally must be added later
  3. catch has a set of brackets (NullPointerException) The purpose is to catch a certain type of exception
  4. 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
  5. 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

  1. Exceptions can only be thrown on methods. Attributes cannot be thrown
  2. Both methods and constructors can throw exceptions, not blocks
  3. Methods can throw more than one exception separated by commas
  4. 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);
    }
}
Published 69 original articles · Like 57 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/qq_43598138/article/details/105601590