Home >Java >javaTutorial >Completely master the principles and applications of Java exception handling mechanism
This article brings you relevant knowledge about java, which mainly introduces the principles and application-related issues of the exception handling mechanism, including Exception and Error, trycatch syntax, and trycatch execution order Wait, I hope it helps everyone.
Recommended study: "java Learning Tutorial"
Everyone may be interested in trycatch They are no strangers to it, and they are all very proficient in using it.
When an error occurs during the running of the program, an exception will be thrown. It is better to throw an exception than to terminate the program.
You can also perform a trycatch operation when it is known that an error is about to occur, and perform certain special operations when an exception occurs.
Exception and Error both inherit from the Throwable class. In Java, only instances of the Throwable type can be thrown or caught. It is The basic component types of exception handling mechanisms.
Exception is a predictable abnormal situation, which can be obtained and processed outside the business.
Error is an unpredictable exception. When an error occurs, it will directly cause the JVM to be unable to handle it.
Exception is divided into checked exceptions and non-checked exceptions.
Checked exceptions must be caught using try catch when writing code (for example: IOException exception).
With non-checked exceptions, the compiler will not find out whether this will occur once, such as a null pointer exception. This kind of exception can be avoided through specification during code writing or use. For example, the findbugs function of sts can detect null pointer exceptions in the code.
NoClassDefFoundError is an error thrown by the JVM when it cannot find the corresponding class when loading a class through classpath.
ClassNotFoundException: If this exception may occur during compilation, it must be thrown during compilation.
NoClassDefFoundError occurrence scenarios:
ClassNotFoundException scenario:
public static void main(String[] args) { try { Class.forName("test"); } catch (ClassNotFoundException e) { e.printStackTrace(); }}
try statement is used Braces enclose a section of code that may throw one or more exceptions.
The parameters of the catch statement are similar to the declaration of a method, including an exception type and an exception object. The exception type must be a subclass of the Throwable class, which specifies the exception type processed by the catch statement. The exception object is generated and captured by the runtime system in the code block specified by try. The curly braces contain the processing of the object, where Object methods can be called.
There can be multiple catch statements to handle different types of exceptions respectively. The Java runtime system detects the exception type handled by each catch statement from top to bottom until a catch statement with a matching type is found. Here, type matching means that the exception type handled by catch is exactly the same as the type of the generated exception object or its parent class. Therefore, the order of catch statements should be from special to general.
Regardless of whether an exception is thrown in the try, the code in the finally statement will be executed. The most important function of the finally statement block should be to release Resources applied for.
throws always appears after the function header to indicate exceptions that may be thrown by the method.
is similar to throws, but the location is different. throw is placed in the catch module. The program will terminate immediately after the throw is executed. The code after throw No longer executed, except finally.
public void test() throws Exception{ throw new Exception();};
try{ //代码区}catch(Exception e){ log.error("error: {}", e);}finally{ //最后必须执行的部分}
Start execution from the first line of code in try. When the code that causes an exception is executed, the JVM will create an exception object.
Determine whether catch can capture the exception object created by jvm.
If caught, jump to the catch code block without ending the program and continue with the code logic in catch;
If it cannot be caught, print the exception information directly and end the program.
If there is no exception in try, the code in try will be executed, skip the catch, and enter the finally code block.
If an exception that needs to be detected is thrown in a method, it must be declared in the method, otherwise it must be caught with try-catch in the method, otherwise the compilation will fail.
If the function declaring an exception is called, either try-catch or throws, otherwise the compilation fails.
When to catch and when to throw? The functional content can be solved, but it cannot be solved by using catch. Use throws to tell the caller that there is a caller to solve it.
If a function throws multiple exceptions, there must be multiple corresponding catches for targeted processing when calling.
Recommended study: "java tutorial"
The above is the detailed content of Completely master the principles and applications of Java exception handling mechanism. For more information, please follow other related articles on the PHP Chinese website!