In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the principle of java exception handling mechanism and application example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
I. brief introduction of Java exception
Everyone may be no stranger to trycatch, and they are very proficient in using it.
When an error occurs while the program is running, an exception will be thrown, and it is better to throw an exception than to terminate the program.
Trycatch operations can also be performed when an error is known to occur, and some specific actions can be performed in the event of an exception.
1. Exception and Error
Both Exception and Error inherit from the Throwable class, and only instances of type Throwable can be thrown or captured in Java, which is the basic component of the exception handling mechanism.
Exception is a predictable exception that can be obtained and handled outside the business.
Error is an unpredictable exception, and after the occurrence of error, it will directly cause the JVM to be unhandled.
Exception can be divided into inspective abnormality and non-examinative abnormality.
To check for sexual exceptions, you must use try catch catch (for example, IOException exceptions) when writing code.
Non-checking exceptions, the compiler will not find out whether this place will be generated once, such as a null pointer exception, which can be avoided by specification during code writing or use. For example, the findbugs function of sts can detect null pointer exceptions in code.
2. What's the difference between NoClassDefFoundError and ClassNotFoundException?
NoClassDefFoundError is an error that is thrown when the corresponding class is not found when the JVM runtime loads the class through classpath.
ClassNotFoundException: if this exception may occur during compilation, it must be thrown during compilation.
The occurrence scenario of NoClassDefFoundError:
The class or jar that the class depends on does not exist
Class files exist, but in different domains, in short, cannot be found
The occurrence scenario of ClassNotFoundException:
When calling the forName method of class, the specified class was not found
The specified class was not found when the findSystemClass () method in ClassLoader
Public static void main (String [] args) {try {Class.forName ("test");} catch (ClassNotFoundException e) {e.printStackTrace ();}} II, trycatch Syntax 1, try statement
The try statement contains a piece of code in curly braces that may throw one or more exceptions.
2. Catch statement
The parameters of the catch statement are similar to the declaration of the method, including an exception type and an exception object. The exception type must be a subclass of the Throwable class, which indicates the type of exception handled by the catch statement, the exception object is generated and captured by the runtime system in the block of code specified by try, and the curly braces contain the handling of the object, where the object's methods can be called.
There can be multiple catch statements that handle different types of exceptions. The Java runtime system detects the exception types 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 or is the parent of the generated exception object, so the order of catch statements should be from special to general.
3. Finally statement
The code in the finally statement executes regardless of whether an exception is thrown in the try, and the most important role of the finally statement block should be to release the requested resources.
4. Throws statement
The throws always appears after the function header to indicate the exceptions that may be thrown by the method.
5. Throw statement
The same thing as throws, except that the location is different, throw is placed in the catch module, the program will be terminated immediately after throw execution, and the code after throw will not be executed, with the exception of finally.
6. Throw exception public void test () throws Exception {throw new Exception (); 7. Catch exception try {/ / code area} catch (Exception e) {log.error ("error: {}", e);} finally {/ / the last part that must be executed} III. Execution order of trycatch
JVM creates an exception object from the first line of code in try to the code where an exception occurs.
Judge whether catch can catch the exception object created by jvm.
If captured, jump to the catch code block, will not end the program, continue from the code logic in catch
If it cannot be captured, print the exception message directly and end the program.
If there are no exceptions in the try, execute the code in try, skip the catch, and enter the finally code block.
IV. Principles of exception handling
If an exception is thrown within a method that needs to be detected, it must be declared on the method, otherwise it must be caught with try-catch within the method, or the compilation will fail.
If a function that declares an exception is called, either try-catch or throws is called, or the compilation fails.
When catch, when throws? Functional content can be solved, with catch, can not be solved, using throws to tell the caller, there is a caller to solve.
If a function throws multiple exceptions, it must be specifically handled by multiple catch when it is called.
Thank you for reading this article carefully. I hope the article "sample Analysis of the principle and Application of java exception handling Mechanism" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.