Java common exceptions and Treatment

Common Java exception handling scheme

 

 

Exceptions are some errors in the program, but not all errors are abnormal and wrong sometimes be avoided.

For example, your code less a semicolon, then the result is running out tips are wrong java.lang.Error; if you use System.out.println (11/0), then you are because you used to do a divisor 0 It will throw java.lang.ArithmeticException exception.

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

In an unusual generate Java, mainly in the following three reasons:

1: Java internal error exception occurs abnormal Java virtual machine generated.

2: abnormal program code written in an error generated, for example, a null pointer exception, abnormal array bounds. This anomaly is called abnormal unchecked, generally need to focus to handle them in some classes.

3: Abnormal throw statement manually generated, the disorder known as abnormality check, typically used to inform the caller of the method of the necessary information.

These anomalies either because of user error caused some procedural errors caused, because there are other physical errors. In order to promptly and effectively handler

Java object-oriented methods to handle the exception. During operation of a method, if an abnormality has occurred, then the method will generate an object representative of the exception, and sent it to the runtime system, the system looks for the appropriate code to handle the runtime exception.

 

We generate an exception object and submit it to the runtime system is called throw ( the throw) exception. The system looks at run-time call stack method, able to handle exceptions until you find the type of object, which is a process known as capture ( the catch) exceptions.

 

Java exception forces the user to consider the robustness and security of the program. Exception handling should not be used to control the normal flow of the program, its main role is to capture a program exception occurs during operation and corresponding processing. Write code to handle exceptions a method that may arise, we can follow the following three principles:

Used in the current method declaration try catch statement to catch the exception.

When a method is covered, covering it the same method must throw an exception or exception subclass.

If the parent class throws more exceptions, the override method must throw a subset of those unusual, but can not throw a new exception. Run-time error, the Java introduces a special exception class.

Here are some common exceptions and solutions:

 

Null pointer exception: NullPointerException. This exception we have developed the most common abnormality, and results in the following diagram, for example, is actually a blog This object is null, when the time is null call the property or method will be reported null pointer exception.

The so-called pointer, is quoted in java objects. For example String s; this s that pointer. 2. The so-called null pointer, the pointer is null contents, such as the above s, so if it points to null, the pointer is null. 3. The so-called null-pointer exceptions, is a pointer to a null pointer, you have to operate it, since it points to an empty object, you can not use this object. The above example s is if null, you have to use a method of s, such s.equals (String x); it will produce a null pointer exception

Null pointer exception solution are:

1 : Comparison with string literals

 In the application code is a common case is compared with a variable string literal. A string literal may be a string or enumeration element. We will call the method by using a literal, rather than by using a null object to call methods

2. Check the parameters of a method
  before executing the method, be sure to check whether the parameter is null. When the parameters are properly checked, the method will continue. Otherwise, you can throw IllegalArgumentException and notification of incoming calls the method parameter is incorrect.

3 priority use String.valueOf () instead of toString () 

When your application code string to be described as an object, the object is to avoid the use toString method. If your object reference is null, NullPointerException will be thrown. On the contrary, consider using a static method String.valueOf (), this method does not throw any exception and as a function of the parameters in the case of print null null.

4. Use ternary operator

The ternary operator can help us avoid NullPointerException operator has the form:

boolean expression ? value1 : value2;

The ternary operator can help us avoid NullPointerException operator has the form: First, compute Boolean expression, if the expression is true, value1 is returned, otherwise value2 is returned. We can use the ternary operator to handle a null pointer, for example:

  String message = (str == null) ? “” : str.substring(0, 10);

  Variable message will be empty, if str references to null, otherwise, if str points to the actual data, message will get before str of 10 characters.

5. Create a return-empty rather than null method

A very good technique is to create a way to return empty collection instead of returning a null value. Your application code can iterate empty set and use its methods and fields, but does not throw NullPointerException

Type cast exception: ClassCastException This error is very common, this error occurs when casts usually appear in the program.

Cross-border array subscript: ArrayIndexOutOfBoundsException. Length of the array determines the amount of data it can load, such as a multi-array length is 4, the array is from 0 starts counting, but the call is indeed 7, beyond the range of the array, so the error.

hql or sql query the exception: SQLException or hql.ast.QuerySyntaxException. This is hql or sql exception that you write sql or hql there is an error, you need to modify the debugging.

 

404. Wrong path. It appears 404 must be your request path in question, or the file path does not exist.

 

Memory overflow: OutOfMemoryError. There is a dead cycle code (treatment cycle) is set too startup parameter (parameter setting server startup increases) a small amount of data loaded in memory at one time is too large (to reduce data load time), object set type applications not the recoveries (manual recovery is not subject to collection) that abnormalities caused by very serious, according to the teacher has said. US seventies launch of the spacecraft, because of this explosion cause the system to crash.

Guess you like

Origin www.cnblogs.com/wendi/p/11787149.html