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 introduces the relevant knowledge of "what are the common exceptions in Java programs and how to handle them?" in the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Common anomalies in JDK
1. Summary diagram of common anomalies
2. Exception classification in java
The Throwable class has two direct subclasses:
(1) Exception: problems that occur can be captured
(2) Error: system error, usually handled by JVM
3. Classification of caught exceptions
(1) Check exception: an exception class derived from Exception that must be caught or redeclared and thrown
(2) Runtime exception: an exception class derived from RuntimeException. Using throw statement, you can throw this exception object throw new ArithmeticException (…) at any time.
Second, the cause of the exception
1. The user entered illegal data.
2. The file to be opened does not exist.
3. Network communication is a disconnection.
4. JVM memory overflow
Third, several common exceptions in java programs and the reasons for this exception.
1. Java.lang.NullpointerException (null pointer exception)
Reason: this exception is often encountered because there is a null pointer in the program, that is, an uninitialized object or an object that does not exist is called in the program.
It often appears in the code of creating objects and calling arrays, such as the object is not initialized, or the path error when the picture is created, and so on. Logarithmic array code
The occurrence of a null pointer confuses the initialization of an array with the initialization of array elements. The initialization of an array allocates space to the array, while the
Initialization, which assigns an initial value to the elements in the array
2. Java.lang.ClassNotFoundException (the specified class does not exist)
Reason: this exception is thrown when you try to convert a String type data to a specified numeric type, but the string does not meet the requirements of numeric data.
Chang. For example, when converting String-type data "123456" into numeric data, it can be converted. But if the data of type String contains
Non-numeric characters, such as 123-56, will cause an exception when converted to a numeric type. The system will catch the exception and handle it.
3. Java.lang.ClassNotFoundExceptio (the specified class does not exist)
Reason: because the name and path of the class are incorrect, an exception may be thrown when the program tries to load a class through a string. For example:
An exception occurs when calling Class.forName (), or calling ClassLoad's finaSystemClass (), or LoadClass ()
4. Java.lang.IndexOutOfBoundsException (array subscript out of bounds exception)
Reason: check whether the subscript value of the array or string called in the program is out of the range of the array. Generally speaking, it is not easy to show the calling array.
There is also a situation where the length of the array defined in the program is determined by some specific method, not by
As stated in advance, you can check the length of the array at this time to avoid this exception.
5. Java.lang.IllegalArgumentException (the parameter of the method is incorrect)
For example, if there are three values in the method g.setColor (int red,int green,int blue), this exception will occur if there are more than 255. if the program
If this exception exists in the order, check to see if there is anything wrong with the parameter passed or parameter value in the method call.
6. Java.lang.IllegalAccessException (no access permission)
This exception occurs when the program wants to call a class but the current method does not have access to the class. If Package is used in the program
It is possible to have this exception.
7. Java.lang.ArithmeticException (abnormal mathematical operation)
Such an exception occurs when an operation such as dividing by zero occurs in a mathematical operation.
8. Java.lang.ClassCastException (data type conversion exception)
This exception occurs when an attempt is made to force a downward conversion on an object that is not convertible or cannot be converted to an instance of its subclass
9. Java.lang.FileNotFoundException (no exception was found in the file)
This exception is thrown when the program opens a file that does not exist for reading and writing. The exception is made by FileInputStream,FileOutputStream
RandomAccessFile's constructor declaration throws that even if the manipulated file exists, it is inaccessible for some reason, such as opening a
If you only have read-only permission and write data to it, the above construction method will still throw an exception
10. Java.lang.ArrayStoreException (array storage exception)
An exception is thrown when an attempt is made to store an object of an incompatible type into an Object [] array
11. Java.lang.NoSuchMethodException (there is no exception in the method)
When a program tries to create an object through reflection, it accesses (modifies or reads) a method, but an exception is thrown when the method does not exist.
12. Java.lang.EOFException (the file has ended exception)
An exception is thrown when the program encounters the end of a file or stream during input. So this exception is used to check whether the end of the file or stream has been reached.
13. Java.lang.InstantiationException (instantiate exception)
Raised when an attempt is made to create an instance of a class through the newInstance () method of Class, but the program cannot create the object through the constructor.
The Class object represents an abstract class, interface, array class, and primitive type. The class represented by the Class does not have a corresponding constructor.
14. Java.lang.InterruptedException (aborted exception)
This exception is thrown when a thread is in a long wait, sleep, or other paused state while another thread terminates the thread through the Thread's interrupt method.
15. Java.lang.CloneNotSupportedException (cloning exception is not supported)
When the Cloneable interface is not implemented or the cloned method is not supported, calling its clone () method throws the exception
16. Java.lang.OutOfMemoryException (out of memory error)
This error is thrown when there is not enough memory available for the Java virtual machine to allocate to an object
17. Java.lang.NoClassDefFoundException (class definition error not found)
This error is thrown when the Java virtual machine or class loader attempts to instantiate a class and cannot find the definition of the class
This is the end of the content of "what are the common exceptions in Java programs and how to handle them?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.