Home  >  Article  >  Java  >  Several common exceptions in java

Several common exceptions in java

尚
Original
2019-12-26 14:01:177120browse

Several common exceptions in java

Abnormal, according to the literal meaning, means unexpected. To understand it at the code level, it prevents the current method or scope from continuing to execute. In Java, exceptions are handled as objects, and their base class is Throwable.

Several common exceptions in java:

1. Null pointer exception class: NullPointerException

An uninitialized object is called or does not exist Object. It often appears in operations such as creating images and calling arrays. For example, the image is not initialized, or the path when creating the image is wrong, etc. A null pointer appears during 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 to the array, and the elements in the initialized array have not been instantiated and are still empty, so each element needs to be initialized (if you want to call if).

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

When trying to force downcasting on an object, but the object cannot be converted and This exception is thrown when an instance is not convertible to its subclass, as in the following code.

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

3. No access rights: java.lang.IllegalAccessException

When the application wants to call a class, but the current method does not have access rights to the class This exception will occur. Please pay attention to this exception when using Package in the program.

4. Method parameter error: java.lang.IllegalArgumentException

For example, three of the methods g.setColor(int red, int green, int blue) If the value exceeds 255, this exception will also occur. Therefore, once we find this exception, what we have to do is to quickly check whether there is an error in the parameter passing in the method call.

5. Array subscript out-of-bounds exception: java.lang.IndexOutOfBoundsException

Check whether the subscript value of the called array or string exceeds the range of the array. Generally speaking, explicit (that is, using a constant as a subscript directly) calls are less likely to make such errors, but implicit (that is, using variables to represent subscripts) calls often cause errors.

There is another situation where the length of the array defined in the program is determined by some specific methods and is not declared in advance. At this time, check the length of the array first to avoid this exception.

6. The file has ended exception: EOFException

When the program encounters the end of the file or stream during the input process, an exception is thrown. Therefore, this exception is used to check whether the end of the file or stream has been reached

7. File not found exception: FileNotFoundException

When the program attempts to open a non-existent file for reading This exception will be thrown when writing. This exception is thrown by the constructor declaration of FileInputStream, FileOutputStream, and RandomAccessFile. Even if the file being operated exists but is inaccessible for some reason, such as opening a read-only file for writing, these construction methods will still throw an exception.

8. Exception when converting string to number: NumberFormatException

When trying to convert a String to a specified number type, and the string does not meet the number type requirements format, this exception is thrown. For example, when character data "123456" is converted into numeric data, it is allowed.

But if the character data contains non-numeric characters, such as 123#56, an exception will occur when converting to 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 the program attempts to An exception may be thrown when loading a class via a string. For example: call Class.forName; or call ClassLoad's finaSystemClass; or LoadClass;

10. Instantiation exception: java.lang.InstantiationException

When trying to pass Class The newInstance method creates an instance of a class, but the program cannot create the object through the constructor. Class object represents an abstract class, interface, array class, basic type. The class represented by this Class does not have a corresponding constructor.

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Several common exceptions in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn