Abnormal study notes


1. Types of exceptions

Checked exception

  • Compile-time exception, you must handle the exception before you can run it;
  • The exceptions defined in Java's standard packages java.lang, java.util and java.net are non-operational exceptions.

Unchecked exception

  • runtime exception;
  • Errors caused by division by 0 in the program, array subscript out-of-bounds error, wrong cast error, use of null object NullPointerException, etc. Although they are exceptions caused by the program itself, they are not actively thrown by the program, but in generated while the program is running.

Common exception classes

insert image description here

  • Arithmetic exception class: ArithmeticExecption

  • Null pointer exception class: NullPointerException

  • Type casting exception: ClassCastException

  • Array negative subscript exception: NegativeArrayException

  • Array index out of bounds exception: ArrayIndexOutOfBoundsException

  • File not found exception: FileNotFoundException

  • String to Number Exception: NumberFormatException

  • Operation database exception: SQLException

  • Input and output exception: IOException

  • Method not found exception: NoSuchMethodException

2. Defensive programming

  • LBYL: look brfore your leap. Be well prepared before the operation;
  • EAFP: it's easier to ask forgiveness than permission. Operate first, deal with problems

3. Basic usage of exception

try {
    
    
	有可能出现异常的语句;
} catch(异常类型 异常对象) {
    
    
	出现异常后的行为;
}
finally {
    
    
	异常的出口
}
  • Different exceptions may be thrown in a piece of code, and different exceptions will be handled differently, so multiple catch code blocks can be used, but only one can be executed; you can also use catch (NullPointerException | ArithmeticExecption e) {} ;
  • Finally, you can use try to recycle resources directly without using it;
  • If there is no suitable exception handling method in the method, it will be passed up the call stack;

Fourth, the exception handling process

Execute the code in try first; if there is an exception in the code in try, the code in try will end, and the corresponding exception type will be found in the catch code block. If found, the code in catch will be executed; if not found, it will be executed. It will be passed up to the upper-level caller; until the main method has no suitable code to handle the exception, it will be handed over to the JVM for processing, and the program will terminate abnormally. The finally block executes before the method ends, whether or not a matching exception type is found.

The difference between throw and throws

throw is a statement that throws an exception; throws is a statement that a method may throw an exception. (When declaring a method, it means that the method may throw an exception)

The difference between final, finally, and finalize

  • final is used to decorate variables, methods and classes.

    • Final variable: Modified variables are immutable. Immutability is divided into reference immutability and object immutability. Final refers to reference immutability. Final modified variables must be initialized. Modified variables are usually called constants.
    • final method: The modified method is not allowed to be overridden by any subclass, and subclasses can use this method.
    • final class: The modified class cannot be inherited, and all methods cannot be overridden.
  • finally As part of exception handling, it can only be used in a try/catch statement, and with a statement block indicating that this statement must be executed eventually (whether an exception is thrown or not), it is often used in situations where resources need to be released, System .exit (0) blocks finally execution.

  • finalize is a method defined in java.lang.Object, which means that every object has such a method, which is called when gc starts and the object is recycled.
    The finalize method of an object will only be called once, and the object may not be reclaimed immediately after finalize is called, so it is possible that after finalize is called, the object does not need to be reclaimed, and then when it really needs to be reclaimed, because the previous call Once, so finalize will not be called again, which will cause problems, so the finalize method is not recommended.

In try-catch-finally, if the catch returns, will finally be executed?

  • The finally statement is executed before any return statement in try or catch, if finally exists. If there is a return statement in finally, the program will return, so the return in finally will be returned.

Guess you like

Origin blog.csdn.net/ccyzq/article/details/121788067