Joke Collection Website - Joke collection - Several common exceptions in java

Several common exceptions in java

1, NullPointerException class: nullpointerexception

An uninitialized object or a nonexistent object was called. It often appears in the operations of creating pictures and calling arrays, such as uninitialized pictures or wrong paths when creating pictures. Null pointer appears in array operation, which confuses the initialization of array and the initialization of array elements.

The initialization of the array is to allocate the required space for the array, and the elements in the initialized array are still empty without instantiation, so each element needs to be initialized (if it is to be called).

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

This exception is thrown when an attempt is made to convert an object down, but the object can neither be converted to an instance of its subclass, as shown in the following code.

Object obj? =? New? Integer (0);

String str = obj

3. No access right: java.lang.illegalaccessexception.

This exception occurs when an application wants to call a class, but the current method does not have permission to access the class. Note this exception when using a package in a program.

4. Parameter error of method: java.lang.illegalargumentexception.

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

5. Array subscript out of bounds exception

Check whether the subscript value of the called array or string is out of the range of the array. Generally speaking, it is not easy to make such mistakes in display (that is, directly using constants as subscripts) calls, but it is often wrong in implicit (that is, using variables to indicate subscripts) calls.

In another case, the length of the array defined in the program is determined by some specific methods, rather than being declared in advance. At this point, first check the length of the array to avoid this exception.

6. Exception at the end of the file: EOFException

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

7. No exception was found in the file: FileNotFoundException.

This exception is thrown when a program tries to open a file that does not exist for reading and writing. Exceptions are thrown by file input streams, fileoutputstream, and constructors of random access files. Even if the manipulated file exists, but it is inaccessible for some reason, such as opening a read-only file for writing, these constructors will still throw an exception.

8. The string is converted into a numeric exception: NumberFormatException.

This exception is thrown when you try to convert a string to the specified numeric type, but the string does not conform to the format required by the numeric value type. For example, character data "123456" is allowed to be converted into numerical data.

However, if the character data contains non-numeric characters, such as 123#56, an exception will occur when converting to digital data. The system will catch and handle this exception.

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, when a program tries to load a class through a string, it may throw an exception. For example: call Class.forName or call ClassLoad's finaSystemClass or load level;

10, instantiation exception: java.lang.instantiationexception

Thrown when an attempt is made to create an instance of a class through its newInstance method, but the program cannot create an object through the constructor. Class objects represent abstract classes, interfaces, array classes and basic types. The class represented by this class has no corresponding constructor.