[Java Core Technology] Exception Handling

insert image description here

1. Abnormal Architecture

  • java.lang.Throwable
    • java.lang.Error: generally do not write targeted code for processing
    • java.lang.Exception: can handle exceptions
      • Compile-time exception (checked)
        • IOException
          • FileNotFoundException
        • ClassNotFoundException
      • Runtime exception (unchecked)
        • NullPointerException
        • ArrayIndexOutOfBoundsException
        • ClassCastException
        • NumberFormatException
        • InputMismatchException
        • ArithmeticException

2. The first method of exception handling: try-catch-finally

try {
  //可能出现异常的代码
} catch (异常类型1 变量名1) {
  //处理异常的方式1
} catch (异常类型2 变量名2) {
  //处理异常的方式2
} catch (异常类型3 变量名3) {
  //处理异常的方式3
}
......
finally {
  //一定会执行的代码
}

Description of try-catch-finally

  • Use try to wrap possible exception codes. During the execution process, once an exception occurs, an object corresponding to the exception class will be generated, and according to the type of the object, it will be matched in the catch
  • Once the exception object in try matches a certain catch, it enters the catch to handle the exception. Once the processing is completed, jump out of the current try-catch structure (in the case of not writing finally). Continue to execute the following code
  • If the exception type in the catch does not have a child-parent relationship, it doesn’t matter who declares it above and who declares it below; if the child-parent relationship is satisfied, the subclass must be declared above the parent class, otherwise an error will be reported.
  • Common exception object handling methods:
    • String getMessage()
    • printStackTrace()
  • Variables declared in the try structure cannot be called after the try structure is released
  • The try-catch-finally structure can be nested

The experience of try-catch-finally

  • Use try-catch-finally to handle compile-time exceptions, so that the program will no longer report errors at compile time, but errors may still be reported at runtime. It is equivalent to using try-catch-finally to delay an exception that may occur at compile time until runtime.
  • In development, because runtime exceptions are more common, we usually don't write try-catch-finally for runtime exceptions. For compile-time exceptions, we must consider exception handling.

finally's description

  • finally is optional
  • The statement in finally is the code that must be executed. Even if an exception occurs again in the catch, there is a return statement in the try, and there is a return statement in the catch.
  • For resources such as database connections, input and output streams, and network programming sockets, the JVM cannot automatically recycle them. We need to manually release resources by ourselves. The resource release at this time needs to be declared in finally.

3. The second method of exception handling: throws + exception type

  • "throws + exception type" is written at the declaration of the method. Indicates the type of exception that may be thrown when this method is executed. Once an exception occurs when the method body is executed, an object of the exception class will still be generated at the exception code. When this object meets the exception type after throws, it will be thrown. The code following the exception code will no longer be executed!
  • try-catch-finally: The exception is really handled; the way of throws just throws the exception to the caller of the method, and does not really handle the exception.
  • How to choose to use try-catch-finally or throws in development?
    • If the overridden method in the parent class does not have throws to handle exceptions, the method overridden by the subclass cannot use throws, which means that if there is an exception in the overridden method of the subclass, it must be handled by try-catch-finally
    • In the executed method a, several other methods are called successively, and these methods are executed in a progressive relationship. We recommend that these methods use throws for processing. The executed method a can be considered to be processed in a try-catch-finally manner.

4. Custom exception class

  • Inherited from existing exception structure: RuntimeException, Exception
  • Provide a global constant: serialVersionUID
  • Provides an overloaded constructor

5. Additional: shortcut keys in Eclipse

  • Declaration of code completion: alt + /
  • Quick retouch: ctrl + l
  • Batch import package: ctrl + shift + o
  • Use a single line comment: ctrl + /
  • Use multi-line comments: ctrl + shift + /
  • Cancel multi-line comment: ctrl + shift + \
  • Copy the code of the specified line: ctrl + alt + down or ctrl + alt + up
  • Delete the code of the specified line: ctrl + d
  • Move code up and down: alt + up or alt + down
  • Switch to the next line of code space: shift + enter
  • Switch to the previous line of code space: ctrl + shift + enter
  • How to view the source code: ctrl + select the specified structure or ctrl + shift + t
  • Return to the previous edited page: alt + left
  • Go to the next edit page (for the one above): alt + right
  • Select the specified class with the cursor to view the inheritance tree structure: ctrl + t
  • Copy code: ctrl + c
  • Undo: ctrl + z
  • Undo: ctrl + y
  • cut: ctrl+x
  • Paste: ctrl + v
  • Save: ctrl + s
  • Select all: ctrl + a
  • Format code: ctrl + shift + f
  • Select several lines and move backward as a whole: tab
  • Select several lines and move forward as a whole: shift + tab
  • In the current class, display the class structure and support searching for specified methods, attributes, etc.: ctrl + o
  • Modify the specified variable name, method name, class name, etc. in batches: alt + shift + r
  • Switch the case of the selected structure: change to uppercase: ctrl + shift + x
  • Switch the case of the selected structure: change to lowercase: ctrl + shift + y
  • Call out structures such as getter/setter/constructor: alt + shift + s
  • Display the properties of the currently selected resource (project or file): alt + enter
  • Quick search: refer to the selected Word to quickly navigate to the next one: ctrl + k
  • Close the current window: ctrl + w
  • Close all windows: ctrl + shift + w
  • View the places used by the specified organization: ctrl + alt + g
  • Find and replace: ctrl + f
  • Maximize the current View: ctrl + m
  • Position directly to the first position of the current line: home
  • Position directly to the end of the current line: end

Guess you like

Origin blog.csdn.net/qq_51808107/article/details/131428791