Common exceptions in Java

Source: Internet
Author: User

1. java.lang.NullPointerException


This exception everyone must often encounter, the exception of the explanation is "the program encountered a null pointer ", simply called an uninitialized object or a non-existent object , this error often occurs in the creation of images, call array of these operations, the chip is not initialized, or a path error when creating a picture, and so on. A null pointer appears in an array operation, and in many cases is a common mistake made by friends who are just beginning to learn programming, which confuses the initialization of arrays and the initialization of the array elements. The initialization of an array is the space required for the allocation of arrays, and the initialized array, where the elements are not instantiated, are still empty, so you also need to initialize each element (if it is to be called).

2. java.lang.ClassNotFoundException


This anomaly is a lot of developers originally developed in the development environment such as JB, put JB under the package under the WTK to compile frequently problems, the explanation of the exception is "The specified class does not exist ", here the main consideration is the name and path of the class is correct, if it is in JB under the package, It is usually the default plus package, so go to the WTK and pay attention to add the package path.

3. Java.lang.arithmeticexception


The explanation of this anomaly is " mathematical operation Anomaly ", for example, the program appears divided by 0 such an operation will be such an exception, for this anomaly, we will have to check the application of their own procedures involved in the mathematical operation of the place, the formula is not inappropriate.

4. Java.lang.arrayindexoutofboundsexception


This exception is believed many friends also often encountered, abnormal explanation is "array subscript out of bounds", now most of the program has an array of operations, so in the call arrays must be carefully checked, to see whether the subscript is beyond the range of the array , in general, The display (that is, directly with the constant subscript) call is not easy to make such a mistake, but the implicit (that is, the use of variable-subscript) calls are often wrong, there is also a case, the length of the array defined in the program is determined by certain methods, is not declared beforehand, it is better to look at the array length, To prevent this exception from occurring.

5. Java.lang.illegalargumentexception


The explanation of this exception is "parameter error of the method", and many methods in the J2ME class library can cause such errors in some cases, such as the volume parameter in the volume adjustment method, if it is written as a negative number, then the exception will occur, such as g.setcolor (int red,int green,int Blue) Three values in this method, if there are more than 255 of the exception will also occur, so once the exception is found, we have to do is to quickly check the method call in the parameter passed is not an error occurred.

6. Java.lang.illegalaccessexception

This exception is interpreted as "no access", which occurs when the application is calling a class, but the current method does not have access to the class. Be aware of this exception in cases where the package is used in the program.

There are many other exceptions, I will not enumerate, I would like to state that a qualified programmer, the program needs to have a good understanding of common problems and corresponding solutions, otherwise just stay in writing procedures and will not change the program, will greatly affect their own development. The full description of the exception is available in the API.

Common exceptions in Java (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.