Feiyun Xia http://www.jscud.com reprinted please indicate the author
[Handle your exceptions]
-----------------
Exception Handling is a very important part in Java programming. It is recommended that you read <negative tive Java programming language guide> or <practical Java> before using exceptions.
Below are some suggestions from the book:
* Never ignore exceptions.
* Never hide exceptions ***
* Exception only when abnormal
* You can check for recoverable usage exceptions and use runtimeexception for program errors)
* Document the exceptions caused by methods
* The detailed information includes the failure capture information.
* Use finally to avoid resource leakage
*....
It is particularly pointed out that null should be specially handled during development. Otherwise, nullpointexception exceptions are often thrown, which is the most troublesome exception in Java.
If your program reports dozens of nullpointexception due to a null value, it is not only annoying, but also difficult to find the error. Therefore, you must pay attention to this problem in Java.
If your function does not allow a null value, you can intercept it, throw an exception, or give the customer a more friendly prompt. Isn't that good?
Let's take an example:
Public String getname (User auser)
{
// What happens if auser is null?
Return auser. getname ();
} |
Obviously, if the parameter is null, an exception is thrown. It should be changed:
Public String getname (User auser)
{
If (null = auser)
{
Return "";
}
Else
{
Return auser. getname ();
}
} |
You can also throw an exception and force the user to enter a null value.
What is often ignored is the difference between runtimeexception and common exception. in Java, this is a special exception class. If this exception is encountered in a program, you can not intercept it, if it is another common exception, you are not allowed to intercept it. our code is often written as follows:
Try
{
// Your code here
}
Catch (exception E)
{
// Do warn
} |
In this way, all exceptions are intercepted, including runtimeexception. In many cases, this is not an appropriate processing method. We should only intercept necessary exceptions, but ignore runtimeexception.
For runtimeexception, there is a better way to use it in spring. We recommend that you read the exception handling code in the Spring framework in transactions, such as the sqlexception conversion thrown by JDBC.
For Exception Handling, I suggest the following:
* When an exception is caught and thrown again, the original exception information must be included.
* Do not forget runtimeexception. Unless necessary, do not catch all exceptions in catch (exception e) mode.
* Do not use exceptions for process control. The performance cost of exceptions is high. (Some may disagree with this. We will not discuss it in detail here)
* Do not throw exception handling to others. If this function is capable of processing, do not throw the exception.
For more information, see <strong tive Java programming language guide> or <practical Java>.
[Excessive dependency]
When Locating errors, I often find that I have browsed seven or eight files, but I still haven't found any place to execute the actually needed functions. This is very depressing. A calls B, B calls C, and C calls d ...... people cannot find the North
In the face of such a program, the problem is not only the trouble of locating errors. If you need to maintain such a function library/framework, I am afraid you have a very high level of control capabilities, otherwise, I will not maintain it.
We 'd better not write such a program for people.
[Abuse interface]
Currently, "interface programming" is popular, which is inherently good, but the abuse of interfaces often occurs.
"Interface-oriented", so all classes have a corresponding interface. The function declaration of the interface is the same as that of the class, and one interface has only one class to implement it. what is the significance of this interface? (Except for transactions using spring)
According to law of demter, an object should have as little knowledge as possible about other objects. only the methods required by the other party should be defined in an interface, rather than some useless method declarations in the interface.
For example, the following class:
Public class mycounter
{
Private int N1;
Private int N2;
Public mycounter (INT N1, int N2)
{
This. n1 = N1;
This. n2 = n2;
}
Public void setn1 (INT N1)
{
Return this. n1 = N1;
}
Public void setn2 (INT N2)
{
Return this. n2 = n2;
}
Public int getn1 ()
{
Return N1;
}
Public int getn2 ()
{
Return N2;
}
Public int getresult ()
{
Return N1 + N2;
}
}
|
We can see that the main purpose of this class is to get the calculation result, so the correct interface should be similar:
Public interface counter
{
Int getresult ();
}
|
But in many cases, this interface is often used:
Public interface counter
{
Int getresult ();
Int getn1 ();
Int getn2 ();
Void setn1 (INT N1 );
Void setn2 (INT N2 );
}
|
Let's think about it. There are two consequences:
1. Except for getresult, other functions are useless, so they are redundant.
2. if we want to implement a counter by ourselves, if the interface only defines getresult, we only need to implement it. our own classes may be multi-number operations, multiplication, division, addition, subtraction, and other operations, and parameters may also be arrays. however, if the interface is declared according to the second method, we must implement the following four methods. If so, it is not only useless but also a waste of time. i'm afraid we're going to yell at our mother.
Therefore, the interface has a good effect, but do not abuse it.
■ If your interface always has only one class implementation, there may be no need to use the interface.
■ Your interface only needs to declare functions used by others.