The abnormal phenomenon of Java programs running is called running errors. According to their nature, it can be divided into two categories: error (Error) and exception (Exception); they have a common parent class (also the top-level parent class of all exceptions): Throwable.
Exception class structure
Error
Error is generated by the JVM and abandoned without processing; such errors are usually not related to the code and the operations executed, and are relatively serious problems in the virtual machine and cannot be solved by the program itself (common errors include dead loops, memory leaks, etc.).
A common error is a Java Virtual Machine Error (VirtualMachineError). When the JVM no longer has the memory resources required to continue the operation, an OutOfMemoryError will appear. When these exceptions occur, the Java virtual machine (JVM) generally chooses to terminate threads.
Exception
Exception is used as an object and is discarded or processed by Java programs; usually the causes of exceptions are: the code or calling code is incorrect, the operating system resources are unavailable, and the common language runtime library encounters an accident;
Exceptions are divided into runtime exceptions generated by virtual machines (RuntimeException, such as null pointers, array out of bounds) and non-runtime exceptions caused by program accidents (such as IOException);
Runtime exception: The program can choose to capture and process or throw it directly without processing; common runtime exceptions include algorithm overflow (exceeding the numerical expression range), divisor is zero, memory overflow, null pointer, invalid parameters, etc. Such exceptions are programming errors in the program itself. You can adjust the running direction of the exception code to keep the program running until it ends normally;
Non-runtime exception: Unexpected exceptions of RuntimeException type are called non-runtime exceptions. From the perspective of program syntax, they are exceptions that must be processed. If they are not processed, the program cannot be compiled and passed; common non-runtime exceptions include IOException, such as: resource files cannot be found, etc., which means that the program cannot execute successfully when encountering an unexpected situation.
Note: The difference between exceptions and errors--Exceptions can be handled by the program itself, but errors cannot be handled.
Checkable exceptions and not checkable exceptions
Java exceptions (ErrorandExceptions) can be divided into two other categories according to whether the program can be compiled and passed: checked exceptions and unchecked exceptions.
Can check exceptions
Exception can be checked: Corresponding to non-runtime exceptions, the compiler can check the expected errors when compiling the program. This exception must be handled. There are two ways to deal with it: the try-catch statement catches the exception or the throws clause declares throwing an exception, and the exception type is declared outside the method's header (using throws Exception) and the caller can choose to handle the received exception.
Speaking of these two ways of handling, we need to talk about the 5 keywords of Java exception handling: try, catch, finally, throws, throw
1. Use the try-catch statement to process capture (the method header does not need to be declared)
The three statement blocks of try, catch, and finally cannot appear alone. The three can be combined into try-catch/try-catch-finally/try-finally.
There can be multiple catch blocks, and at this time, one of the catch block codes can only be matched from top to bottom;
Finally, at most one variable is not accessible to each other in the three statement blocks.
In this section, the try-catch combination is used to handle the checkable exception, throwing the exception upward, example:
public XMLReaderHDU(String xmlFile){super(); this.xmlFile = xmlFile; this.fileName=StaticConfig.umlPathPrefixHDU;try {SAXReader reader=new SAXReader();Document dom=reader.read(xmlFile);root=dom.getRootElement();}catch(Exception e1) {Handle1…}catch(Exception e2) {Handle1…}}2. Use throws clause to declare throwing (declare throwing exception type in the head outside the method body: use throws to throw Exception)
First, use throw inside the method body to throw an exception;
Then, the method is declared outside the head and throws the exception thrown inside using throws;
Example 1: try-catch throws an exception
public XMLReaderHDU(String xmlFile) throws Exception {super(); this.xmlFile = xmlFile; this.fileName=StaticConfig.umlPathPrefixHDU;try {SAXReader reader=new SAXReader();Document dom=reader.read(xmlFile);root=dom.getRootElement();}catch(Exception e1) {throw new Exception(e1);}}Example 2: throw-throws custom exception
private List<UseCase> readUCInformation() throws Exception {if(e1.hasContent()) {if(e1.element("name").getText().equals("preCondition")){uc.setPreCondition(e1.elementText("content"));} else{throw new Exception("UseCase constraint obtain exception, possible cause: constraint type or constraint name error");}}}Unable to check for exceptions
Uncheckable exceptions: Includes error Error and runtime exception RuntimeException
Runtime exceptions can be passed during program compilation. It cannot be detected through static syntax which functions may throw exceptions. Only when the exception is found (that is, depending on the runtime state, it is determined by the runtime state);
You can choose to capture processing or display thrown (mustbecaughtordeclearedtobethrown). These exceptions are generally caused by program logic errors, and programs should avoid such exceptions as much as possible from a logical perspective.
Summarize
The above is all about the analysis of basic knowledge of Java exceptions in this article, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!