In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The main content of this article is "JAVA exception classification and handling methods", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "JAVA exception classification and handling methods" it!
I. JAVA exception classification and handling
1. Concept
If a method cannot complete the task in the normal way, it can exit the method through another path. In this case, an object is thrown that encapsulates the error message. At this point, this method exits immediately without returning any value. In addition, other code that calls this method cannot continue to execute, and the exception handling mechanism hands the code execution to the exception handler.
two。 Anomaly classification
Throwable is the superclass of all errors or exceptions in the Java language. The next layer is divided into Error and Exception
Error
1. The Error class refers to the internal errors and resource exhaustion errors of the java runtime system. The application does not throw such objects. If such an error occurs, in addition to telling the user, all that is left is to try to make the program terminate safely.
Exception (RuntimeException, CheckedException)
2. Exception has two branches, one is runtime exception RuntimeException, and the other is CheckedException. RuntimeException, such as NullPointerException, ClassCastException; one is to check for exception CheckedException, such as IOException and SQLException caused by I _ IOException O error. RuntimeException is a superclass of exceptions that may be thrown during the normal operation of the Java virtual machine.
Check exception CheckedException: generally, it is an external error. This kind of exception occurs during the compilation phase. The Java compiler will force the program to catch this kind of exception, that is, it will require you to try catch the program that may have an exception. This kind of exception generally includes several aspects:
1. Attempt to read data at the end of the file
two。 An attempt was made to open a malformed URL
3. An attempt was made to find a class object based on a given string, and the class represented by this string does not exist
3. The handling of exceptions
There are three forms of throwing an exception: throw, a throws, and a system that automatically throws an exception.
Targeted handling of try catch catch exception
4. The difference between Throw and throws:
Different locations
1. Throws is used in functions, followed by exception classes, which can be followed by multiple, while throw is used in functions, followed by exception objects.
The functions are different:
2. Throws is used to declare exceptions, so that the caller only knows the possible problems with the feature and can give a pre-processing method. When throw throws a specific problem object and executes it to throw, the function ends, jumps to the caller, and throws the specific problem object to the caller. That is, when throw statements exist independently, do not define other statements below, because they cannot be executed.
3. Throws indicates a possibility of exceptions, which may not necessarily occur; throw throws an exception, while executing throw must throw some exception object.
4. Both are negative ways of handling exceptions, only throwing or possibly throwing exceptions, but they are not handled by the function, and the real exception handling is handled by the upper layer of the function.
two。 JAVA reflection
1. Dynamic language
Dynamic language means that a program can change its structure when it is running: new functions can be introduced, existing functions can be deleted, and other structural changes. For example, the common JavaScript is a dynamic language, in addition, Ruby,Python is also a dynamic language, while C, C++ are not dynamic languages. JAVA is a semi-dynamic language from a reflective point of view.
two。 The concept of reflection mechanism (knowing all the properties and methods of the class in the running state)
The reflection mechanism in Java means that in the running state, all the properties and methods of this class can be known for any class, and for any object, any one of its methods can be called; this function of dynamically obtaining information and dynamically calling object methods has become the reflection mechanism of Java language.
3. The application of reflection
Compile-time and run-time types
In Java programs, many objects are run in two types: compile-time type and run-time type. The type at compile time is determined by the type that is useful when the object is declared, and the type at run time is determined by the type actually assigned to the object. Such as:
Person p=new Student (); where the compile-time type is Person and the runtime type is Student.
The compile-time type of cannot get the specific method
The program may also receive an external incoming object at run time, whose compile-time type is Object, but the program needs to call methods of the object's run-time type. In order to solve these problems, the program needs to discover the real information of objects and classes at run time. However, if it is impossible to predict which classes the object and class belong to at compile time, the program can only rely on run-time information to discover the real information of the object and class, and reflection must be used at this time.
4. Java reflection API
Reflection API is used to generate information about classes, interfaces, or objects in JVM.
1. Class class: the core class of reflection, which can obtain the properties, methods and other information of the class.
2. Field class: a class in the Java.lang.reflec package that represents the member variables of the class and can be used to get and set property values in the class.
3. Method class: a class in the Java.lang.reflec package that represents the methods of the class, which can be used to obtain method information in the class or to execute methods.
4. Constructor class: a class in the Java.lang.reflec package that represents the constructor of the class.
5. Reflection usage steps (get Class object, call object method)
1. Get the Class object of the class you want to operate on, which is the core of reflection. Through the Class object, we can call the methods of the class at will.
two。 Calling a method in the Class class is the use phase of reflection.
3. Use reflection API to manipulate this information.
6. Three methods of getting Class object
Call the getClass () method of an object
Person p=new Person ()
Class clazz=p.getClass ()
Call the class property of a class to get the corresponding Class object of that class
Class clazz=Person.class
Use the forName () static method in the Class class (safest / best performance)
Class clazz=Class.forName ("full path to a class"); (most commonly used)
When we have the Class object of the class we want to operate on, we can get and view the methods and properties in the Class class through the methods in the class.
7. Two ways to create an object
NewInstance () of the Class object
1. Use the newInstance () method of the Class object to create an instance of the corresponding class of the Class object, but this method requires the corresponding class of the Class object to have a default empty constructor.
Call the newInstance () of the Constructor object
two。 First use the Class object to get the specified Constructor object, and then call the newInstance () method of the Constructor object to create an instance of the corresponding class of the Class object, which allows you to select the constructor to create the instance.
I. JAVA exception classification and handling
1. Concept
If a method cannot complete the task in the normal way, it can exit the method through another path. In this case, an object is thrown that encapsulates the error message. At this point, this method exits immediately without returning any value. In addition, other code that calls this method cannot continue to execute, and the exception handling mechanism hands the code execution to the exception handler.
two。 Anomaly classification
Throwable is the superclass of all errors or exceptions in the Java language. The next layer is divided into Error and Exception
Error
1. The Error class refers to the internal errors and resource exhaustion errors of the java runtime system. The application does not throw such objects. If such an error occurs, in addition to telling the user, all that is left is to try to make the program terminate safely.
Exception (RuntimeException, CheckedException)
2. Exception has two branches, one is runtime exception RuntimeException, and the other is CheckedException. RuntimeException, such as NullPointerException, ClassCastException; one is to check for exception CheckedException, such as IOException and SQLException caused by I _ IOException O error. RuntimeException is a superclass of exceptions that may be thrown during the normal operation of the Java virtual machine. If RuntimeException occurs, it must be the programmer's fault.
Check exception CheckedException: generally, it is an external error. This kind of exception occurs during the compilation phase. The Java compiler will force the program to catch this kind of exception, that is, it will require you to try catch the program that may have an exception. This kind of exception generally includes several aspects:
1. Attempt to read data at the end of the file
two。 An attempt was made to open a malformed URL
3. An attempt was made to find a class object based on a given string, and the class represented by this string does not exist
3. The handling of exceptions
Do not deal with the problem specifically, but continue to throw it to the caller (throw,throws)
There are three forms of throwing an exception: throw, a throws, and a system that automatically throws an exception.
Targeted handling of try catch catch exception
4. The difference between Throw and throws:
Different locations
1. Throws is used in functions, followed by exception classes, which can be followed by multiple, while throw is used in functions, followed by exception objects.
The functions are different:
2. Throws is used to declare exceptions, so that the caller only knows the possible problems with the feature and can give a pre-processing method. When throw throws a specific problem object and executes it to throw, the function ends, jumps to the caller, and throws the specific problem object to the caller. That is, when throw statements exist independently, do not define other statements below, because they cannot be executed.
3. Throws indicates a possibility of exceptions, which may not necessarily occur; throw throws an exception, while executing throw must throw some exception object.
4. Both are negative ways of handling exceptions, only throwing or possibly throwing exceptions, but they are not handled by the function, and the real exception handling is handled by the upper layer of the function.
two。 JAVA reflection
1. Dynamic language
Dynamic language means that a program can change its structure when it is running: new functions can be introduced, existing functions can be deleted, and other structural changes. For example, the common JavaScript is a dynamic language, in addition, Ruby,Python is also a dynamic language, while C, C++ are not dynamic languages. JAVA is a semi-dynamic language from a reflective point of view.
two。 The concept of reflection mechanism (knowing all the properties and methods of the class in the running state)
The reflection mechanism in Java means that in the running state, all the properties and methods of this class can be known for any class, and for any object, any one of its methods can be called; this function of dynamically obtaining information and dynamically calling object methods has become the reflection mechanism of Java language.
3. The application of reflection
Compile-time and run-time types
In Java programs, many objects are run in two types: compile-time type and run-time type. The type at compile time is determined by the type that is useful when the object is declared, and the type at run time is determined by the type actually assigned to the object. Such as:
Person p=new Student (); where the compile-time type is Person and the runtime type is Student.
The compile-time type of cannot get the specific method
The program may also receive an external incoming object at run time, whose compile-time type is Object, but the program needs to call methods of the object's run-time type. In order to solve these problems, the program needs to discover the real information of objects and classes at run time. However, if it is impossible to predict which classes the object and class belong to at compile time, the program can only rely on run-time information to discover the real information of the object and class, and reflection must be used at this time.
4. Java reflection API
Reflection API is used to generate information about classes, interfaces, or objects in JVM.
1. Class class: the core class of reflection, which can obtain the properties, methods and other information of the class.
2. Field class: a class in the Java.lang.reflec package that represents the member variables of the class and can be used to get and set property values in the class.
3. Method class: a class in the Java.lang.reflec package that represents the methods of the class, which can be used to obtain method information in the class or to execute methods.
4. Constructor class: a class in the Java.lang.reflec package that represents the constructor of the class.
5. Reflection usage steps (get Class object, call object method)
1. Get the Class object of the class you want to operate on, which is the core of reflection. Through the Class object, we can call the methods of the class at will.
two。 Calling a method in the Class class is the use phase of reflection.
3. Use reflection API to manipulate this information.
6. Three methods of getting Class object
Call the getClass () method of an object
Person p=new Person ()
Class clazz=p.getClass ()
Call the class property of a class to get the corresponding Class object of that class
Class clazz=Person.class
Use the forName () static method in the Class class (safest / best performance)
Class clazz=Class.forName ("full path to a class"); (most commonly used)
When we have the Class object of the class we want to operate on, we can get and view the methods and properties in the Class class through the methods in the class.
7. Two ways to create an object
NewInstance () of the Class object
1. Use the newInstance () method of the Class object to create an instance of the corresponding class of the Class object, but this method requires the corresponding class of the Class object to have a default empty constructor.
Call the newInstance () of the Constructor object
two。 First use the Class object to get the specified Constructor object, and then call the newInstance () method of the Constructor object to create an instance of the corresponding class of the Class object, which allows you to select the constructor to create the instance.
At this point, I believe you have a deeper understanding of "JAVA exception classification and handling methods". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.