Common exception handling of JAVA project summary

A, JDK classes associated with aberrant

analysis:

Java Exception Category:

Throwable class has two direct subclasses:

Exception: the problem arises that can be captured;

Error: System error, usually handled by the JVM.

Can be caught exception can be divided into two categories:

(1) Check exception: derived directly from Exception exception classes must be caught or thrown Again

(2) Runtime exception: derived from RuntimeException exception classes. Use the throw statement can always throw this exception objects: throw new ArithmeticException (...);

Second, there are many reasons for an exception occurs, usually contains the following categories:

1. the user enters invalid data.

2. To open a file does not exist.

3. The network communication connection is interrupted, or the JVM memory overflow.

Third, several common anomalies and their causes

. 1, java.lang.NullPointerException (NullPointerException)

Cause: This exception I am sure you have often encountered, unusual interpretation is "caught in a null pointer program" that calls uninitialized object or object does not exist. Often creating a picture, call the array of these operations, such as uninitialized pictures, or pictures to create a path of error, and so on. Null pointer array operation occurs, i.e., the initialization and the initialization of the array elements of the array confused. Initialize the array is allocated space for an array of needs, and the array after initialization, whose elements are not instantiated, is still empty, so it needs to initialize each element (if you want to call it).

2,  a java.lang.ClassNotFoundException (specified class does not exist)

The main reason: the main consideration here about the name and path are correct to class, usually possible exception is thrown when a program attempts to load a class by string. For example: calling Class.forName (); or by calling ClassLoad of finaSystemClass (); or LoadClass ();

. 3,  java.lang.NumberFormatException (string converted to digital abnormal)

Cause: When attempting to convert a String number specified type, and the string does not satisfy the requirements of the format of the digital type, as will now be thrown when speaking character data "123456" is converted into numerical data. It is allowed. However, if the character data type contains a non-numeric characters, such as 56 # 123, then an exception will be when converted into a numerical type. The system will catch this exception and processed.

. 4, a java.lang.IndexOutOfBoundsException (array subscript bounds exception)

Reason: an array index value or a character string is not a call to see beyond the scope of the array, in general, display (i.e., directly labeled with a constant current) call is not easy with such errors, but implicit (i.e., with a variable represents the subscript) calls often wrong, there is a situation, the array is the length of the program is defined by a certain method of decision, not prior notice, this time to check out the length of the array, in order to avoid this anomaly .

. 5, a java.lang.IllegalArgumentException (parameter error method)

For example g.setColor (int red, int, int blue green) this method of three values, if there are more than 255 will also appear this exception, so if it is found that abnormal, we need to do is rush to check method parameters passed in the call is not an error.

6, java.lang.IllegalAccessException (no access)

When an application to call a class, but that is the current method does not have access to this class there will be the exception. In the case of the program with a Package to pay attention to this anomaly.

7, java.lang.ArithmeticException (math abnormal)

When the arithmetic appeared in such operations will be divided by zero out such an exception. 

. 8, a java.lang.ClassCastException (data type conversion exception)

When an object will attempt to enforce downcast, but the object and its non-convertible and non-convertible subclass instance throws the exception, as the following code.

Object obj = new Integer(0);

String str = obj;

9,  java.lang.FileNotFoundException (file not found exception)

When the program tries to open a file that does not exist to read and write will trigger the exception. The exception is thrown by the constructor statement FileInputStream, FileOutputStream, RandomAccessFile, even if the file operation exists, but for some reason inaccessible, such as opening a read-only file for writing, these constructors still throws an exception.

10, java.lang.ArrayStoreException (Abnormal storage array)

When you try to type object type is not compatible with a deposit Object [] array will throw an exception when, as

Object[] obj = new String[3]

obj[0] = new Integer(0);

. 11, java.lang.NoSuchMethodException (method there is no abnormality)

Program tries to create an object by reflection when accessing (read or modify) a method, but this method does not exist will throw an exception.

12,  java.lang.EOFException (file ends abnormally)

When the program encounters the end of a file or stream during input, an exception is thrown. Thus exception for checking whether the end of the file or stream

13 is, java.lang.InstantiationException (Example Exception)

Raised when trying to newInstance Class of () method creates an instance of a class, but was unable to create the object by the constructor. Class object represents an abstract class, an interface, an array type, base type. The Class class indicates no corresponding constructor.

14, java.lang.InterruptedException (aborted abnormal)

Thrown when a thread in a long time waiting, sleeping, or otherwise paused state, at a time when the other thread to terminate the thread by interrupt method of Thread.

15, java.lang.CloneNotSupportedException (clone does not support exception)

When not implement or does not support Cloneable interface cloning method, call its clone () method out of the anomaly.

16, java.lang.OutOfMemoryException (out of memory error)

This error is thrown when the available memory is not sufficient to make the Java virtual machine is assigned to an object.

17, java.lang.NoClassDefFoundException (class definition not found error)

This error is thrown when the Java Virtual Machine or the class loader tries to instantiate a class, but can not find the definition of the class.

IV Summary

 1, several principles Java exception handling is as follows.
      1) Do not discard abnormal, need to be related to treatment after catching exceptions. If you think you can not deal well with the exception, let it continue to spread, spread to other places to deal with, or put into a low-level exceptions into application-level exception to re-thrown.
      (2) catch statement should specify a particular type of exception. Do not capture exceptions should be captured
      (3) release of resources in the finally inside. If finally there will be an exception is thrown, the same need to use try..catch process.
      (4) Do not plug a lot of code inside a try ... catch block, separating each statement and may appear abnormal, respectively catch the exception.
      (5) due to abnormal output may result in incomplete data, so the user needs to take appropriate action, at least of the data to be presented is incomplete

 

Guess you like

Origin www.cnblogs.com/20183544-wangzhengshuai/p/11762714.html