First sight of JAVA-abnormal mechanism-primary evolution 10
What is abnormal
There are some conditions during the operation, which affect the normal operation of the program. The super class is Throwable
classification
Checked exceptions: caused by user errors or problems, unforeseen, and cannot be simply ignored during compilation
Runnability exceptions: may be avoided by programmers, can be ignored at compile time
Error: not an exception, out of programmer control, stack overflow, etc.
error
Jvm Shengchang throws, most errors have nothing to do with the operation performed by the code
outOfMemoryError NoClassDefFoundError
Exception
Important branch: RuntimeException
ArrayIndexOutOfBoundsException
NullPointerException
ArithmeticException
MissingResourceException
ClassNotFoundException
You can choose not to deal with it, usually caused by program logic errors
the difference:
Error is usually a catastrophic fatal error, and the thread will be terminated when it occurs.
exception can be handled by the program
How to deal with the exception
Throw an exception try catch
Catch exception throw/throws
Custom exception
Inherit the exception class
Summary of experience
- When handling runtime exceptions, use logical areas to reasonably avoid them while assisting try catch processing
- After multiple catch blocks, a catch (Exception) can be added to handle exceptions that may be missed
- For uncertain code, try catch can be added to avoid potential exceptions
- Try to handle exceptions as much as possible, instead of simply calling to print out
- How to deal with it should be decided according to different business requirements and exception types
- Try to add a finally statement to release the occupied resources