1. Differences between anomalies and traditional processing mechanisms
The Java exception mechanism has the following advantages over the traditional return code in the form of an upgrade that returns an error via a return value:
1. Abnormal carry more information, easy to analyze, such as carrying the call location information, error details
2. Can save a lot of code
For example, the traditional program to execute a SQL, you need to determine whether the results are correct, how many SQL may have how many judgments, and the exception mechanism, only to intercept a processing outside the database exception can be
3. Can force request judgment, traditional procedure, function return-1,-2,-3 but not necessarily the programmer remembers to judge, but the exception throw SQLException throw Xxxexception, throws how many kinds, outside needs to handle how many kinds of
4. Traditional return 1,2,3,4 are difficult to memorize, and exceptions are similar to enumerations, making it easy for programmers to remember and use
5. The exception can be a layer of not processing, directly thrown to the top, and the return value must be a layer of judgment, and then return, trouble
2. My summary of how to use exceptions in projects
1. In the project such as Java EE, the project program layered processing, the various levels of errors, need to log and as required in the user page display return, therefore, through the exception of the processing to achieve error flow control
2. If in our program, directly using the underlying exception, such as a function throws SQLException, then, if the next layer to add additional processing, you need to throw other similar to the SQLException exception, is bound, the function definition needs to be modified, so, All the places used in this function involve the interception of new exceptions or continue to be thrown, and such modifications are costly
3. Because of the above reasons, in the project, I think it is best to establish their own exception class, inherit from the exception, the functions within the project, if necessary, throw myexception, and the function of the internal need to intercept a variety of anomalies translated into myexcption, you can directly use the constructor, Construct various exceptions as Myexption
4. At the top, that is, struts action, intercept myexption, as needed to return to the display or log, etc.