Simple principle and application __java of Java exception handling mechanism

Source: Internet
Author: User
Tags exception handling

An exception is an abnormal condition or error that occurs when a Java program is running. Just like the normal life of the unexpected events, where, time, can be expressed in an object, Java in object-oriented way to deal with the exception, the program occurs in each of the exceptions are encapsulated into an object to represent, The object contains exception information.

Java classifies exceptions, and different exceptions are represented by different Java classes, all of which are java.lang.Throwable below and derive two subclasses of error and exception,error to represent a serious problem that the program itself cannot overcome or recover from, The procedure is only dead, for example, memory overflow, deadlock and other problems, exception can be overcome or recovery of a problem, which is divided into system anomalies and common anomalies, system anomaly is the problem of software itself, is the development of poor thinking, software users can not overcome and restore the problem, This situation can let the software continue to run, or let the software die, such as null pointer exception (nullpointerexception), type conversion exception (classcastexception), script out of bounds, and so on, common exception is the operation of environmental changes caused by the problem, is a problem that users can overcome, such as network disconnection, insufficient memory.

Java provides a different solution for system exceptions and common exceptions, the compiler forces normal exception try...catch processing, or declares that throws continues to be thrown to the upper calling method, so common exceptions are also called checked exceptions, and system exceptions can be handled or handled, so The compiler does not force the use of try. Catch processing or declared with throws, so system exceptions are also called unchecked exceptions.

NullPointerExceptionNull pointer error java.lang.NullPointerException use the basic Java data type, the value of the variable is either already the default value, and if it is not properly assigned, the program cannot be compiled, so the basic Java data type is used (double, Float,boolean,char,int,long) generally does not cause null pointer exceptions. Thus, null pointer anomalies are mainly related to the operation of the object.

The following is a list of several scenarios where null pointer exceptions may occur and the corresponding solution:
Start using directly regardless of whether the object is empty.

(JSP) Code snippet 1:out.println (Request.getparameter ("username"));

Description: The function of code Snippet 1 is very simple, which is to output the value of the table field "username" entered by the user.
Description: It seems that the above statement cannot find any grammatical errors, and in most cases is not a problem. However, if a user does not provide a value for the form field "username" when entering data, or bypasses the form's direct input in some way, the value of Request.getparameter ("username") is empty (not an empty string, null object). ), the println method of an Out object cannot operate directly on an empty object, so the JSP page where code Snippet 1 is located will throw a "java.lang.NullPointerException" exception. Even if the object may be empty, some methods such as ToString (), Equals (object obj), and so on, are invoked on the Java.lang.Object or object itself.

(JSP) code snippet 2:string userName = Request.getparameter ("UserName");
If (username.equals ("root")) {...}

Description: The function of code Snippet 2 is to detect user-supplied user names and to perform special operations if the user name is "root".
Note: In code Snippet 2, if a user does not provide a value for the form field "username", the string object username to a null value and cannot directly compare a null object to another object, likewise, the JSP page where code segment 2 is thrown ( java.lang.NullPointerException) NULL pointer error.

(JSP) code snippet 3:string userName = Session.getattribute ("Session.username"). ToString ();
Description: The function of code snippet 3 is to remove the Session.username value from the session and assign the value to the string object UserName. Note: In general, if a user has already made a session, there is no problem, but if the application server restarts and the user has not logged on again, it may be that the user closes the browser but still opens the original page. Then, the value of the session is invalidated, causing the value of the Session.username to be empty. The direct execution of the ToString () operation on a null object causes the system to throw (java.lang.NullPointerException) a null pointer exception.
Solution: In order to ensure that the object being manipulated or referenced is not empty, if we want to manipulate or reference an object, we first check that the object is instantiated and not empty, and add processing to the system for the object to be empty. For example, a string object is used to save the results of a user submission, and when an operation involving an object is detected, if it is empty, and after checking that the object is empty, you can then choose one of the following methods:
Processing mode 1) When the object is empty, the Set object value is an empty string or a default value;
Processing mode 2) detects that an object is empty, does not perform an operation at all, and jumps directly into other processing.
Processing 3) To check that the object is empty, prompting the user to operate with an error. Rewrite the code snippet 2 as follows:

Exception Handling method: 1 Check to the occurrence of an exception, set the object value is an empty string or a default value;
2 detect abnormal appearance, do not perform an operation at all, jump directly to other processing.
3) Check to abnormal appearance, prompt user operation error.


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.