Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the common exceptions in Java

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to solve common anomalies in Java". Many people will encounter such a dilemma in the operation of actual cases, 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. Java.lang.NullPointerException (null pointer exception)

An uninitialized object or an object that does not exist is called. It often occurs in operations such as creating pictures and calling arrays, such as uninitialized pictures, or incorrect paths when creating pictures, and so on. A null pointer appears in the array operation, which confuses the initialization of the array with the initialization of the array elements. The initialization of the array is to allocate the required space to the array, and the elements of the initialized array are not instantiated and are still empty, so each element needs to be initialized (if called).

2. The class specified by java.lang.ClassNotFoundException does not exist

The main consideration here is whether the name and path of the class are correct, usually an exception may be thrown when the program tries to load a class through a string. For example: call Class.forName (); or call ClassLoad's finaSystemClass (); or LoadClass ()

3. Convert java.lang.NumberFormatException string to numeric exception

This exception is thrown when an attempt is made to convert a String to a specified numeric type and the string does not meet the format required by the numeric type. For example, it is allowed to convert character data "123456" into numerical data. However, if the character data contains non-numeric characters, such as 123-56, an exception occurs when converted to numeric data. The system catches the exception and handles it.

4. Java.lang.IndexOutOfBoundsException array subscript out of bounds exception

Check whether the subscript value of the called array or string is beyond the scope of the array. Generally speaking, it is not easy to make such an error when calling a display (that is, directly using a constant as a subscript). But implicit (that is, subscript with variables) calls often go wrong, and in another case, the length of the array defined in the program is determined by some specific method, not declared in advance. At this point, it's best to check the length of the array to avoid this exception.

5. The parameter of java.lang.IllegalArgumentException method is incorrect.

For example, if there are three values in the g.setColor (int red,int green,int blue) method, this exception will also occur if there are more than 255. so once we find this exception, what we need to do is to check whether there is an error in the parameter passing in the method call.

6. Java.lang.IllegalAccessException does not have access rights.

This exception occurs when the application wants to call a class, but the current method does not have access to the class. Pay attention to this exception if Package is used in the program.

7. Abnormal mathematical operation of java.lang.ArithmeticException

Such an exception occurs when an arithmetic operation such as dividing by zero occurs.

8. Java.lang.ClassCastException data type conversion exception

This exception is thrown when an attempt is made to force a downward transformation on an object that is neither convertible nor convertible to an instance of its subclass, such as the following code.

1) Object obj = new Integer (0)

2) String str = obj

9. No exception was found in java.lang.FileNotFoundException file

This exception is thrown when the program attempts to open a non-existent file for reading and writing. This exception is thrown by the constructor declaration of FileInputStream,FileOutputStream,RandomAccessFile, and these constructors still throw an exception even if the manipulated file exists but is inaccessible for some reason, such as opening a read-only file for writing.

10. Java.lang.ArrayStoreException array storage exception

An exception is thrown when an attempt is made to store an object of an incompatible type in an Object [] array, such as

1) Object [] obj = new String [3]

2) obj [0] = new Integer (0)

11. There is no exception in the java.lang.NoSuchMethodException 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. There is no exception in java.lang.NoSuchFiledException method.

When a program tries to create an object through reflection, it accesses (modifies or reads) a filed, but an exception is thrown when the filed does not exist.

13. The java.lang.EOFException file has ended the 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.

14. Java.lang.InstantiationException instantiated 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.

15. 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.

16. Java.lang.CloneNotSupportedException does not support cloning exceptions

When the Cloneable interface is not implemented or the cloned method is not supported, the exception is thrown by calling its clone () method.

17. 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.

18. Java.lang.NoClassDefFoundException could not find class definition error

This error is thrown when the Java virtual machine or class loader tries to instantiate a class and cannot find the definition of the class.

Security violation exception: SecturityException

Database operation exception: SQLException

Input / output exception: IOException

Communication exception: SocketException

This is the end of the content of "how to solve common exceptions in Java". Thank you for your 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report