Common exceptions in Java





Two common abnormal diagrams

Guyu





Common exception description
Guyu

1. Null pointer exception class: NullPointerException

A call was made to an uninitialized or nonexistent object. It often occurs in operations such as creating pictures and calling arrays, such as uninitialized pictures, or wrong paths when pictures are created, and so on. A null pointer appears in an 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 for the array, and the elements in the initialized array are not instantiated and are still empty, so each element needs to be initialized (if it is called).



2. Array subscript out of bounds exception: java.lang.IndexOutOfBoundsException

Check whether the subscript value of the called array or string is beyond the range of the array. Generally speaking, it is not easy to make such a mistake when calling explicitly (that is, directly using a constant as a subscript), but implicit (that is, using a variable to represent the following) standard) calls often go wrong.
Another situation is that the length of the array defined in the program is determined by some specific method, not declared in advance. At this time, check the length of the array first to avoid this exception.



3. Data type conversion exception: java.lang.ClassCastException

This exception is thrown when an attempt is made to enforce a downcast on an object that is neither convertible nor an instance of its subclass, as in the following code:

		Object obj = new Integer(0);
		
		String str = obj;


4. No access rights: java.lang.IllegalAccessException

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



5. Method parameter error: java.lang.IllegalArgumentException

For example, if the three values ​​in the method g.setColor(int red, int green, int blue) exceed 255, this exception will also occur, so once we find this exception, what we have to do is to check the method quickly Is there an error in the parameter passing in the call.



6. The file has ended exception: EOFException

Raises an exception when the program encounters the end-of-file or stream while inputting. So this exception is used to check if end of file or stream is reached



7. File not found exception: FileNotFoundException

This exception is thrown when a program tries to open a non-existent file for reading and writing. This exception is thrown by the constructors of FileInputStream, FileOutputStream, and RandomAccessFile. Even if the operated file
exists but cannot be accessed due to some reasons, such as opening a read-only file for writing, these constructors will still throw an exception.



8. The conversion of strings to numbers is abnormal: NumberFormatException

When trying to convert a String to a specified number type, and the string does not meet the format required by the number type, this exception is thrown. For example, when character data "123456" is converted into numeric data, it is allowed of.
However, if the character data contains non-numeric characters, such as 123#56, an exception will occur when converting to a numeric type. The system will catch this exception and handle it.



9. The specified class does not exist: java.lang.ClassNotFoundException

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;



10. Instantiation exception: java.lang.InstantiationException

Raised when trying 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 basic type. The class represented by this Class has no corresponding constructor.












Note:
Likes, comments, and reprints are welcome. Please give the link to the original text in an obvious place on the article page
. Those who know, thank you for reading my article in the vast crowd.
Where is the signature without personality!
For details, please follow me
and continue to update

Scan to have a surprise!
© 2020 11 - Guyu.com | 【All rights reserved】

Guess you like

Origin blog.csdn.net/weixin_49770443/article/details/109444707