Little thinking

zhaozj2021-02-16  59

Little thinking

"There is no completely error-free program in the world," we don't discuss it is a truth. But it is indeed a warning that warns us to avoid errors as much as possible when writing a program.

When the program runs an error, the programmer will exceed the programmer, so that the program "Nanyong North" does not only complete the function normally, but there will be some terrible things.

Due to the number of accidents due to the wrong accident, the most lost is a rocket launch in the United States. In that tragedy, because of the wrong way in the program, "wrote"; ", it makes the rocket explodes in the sky. Therefore, it should be in order to prevent manpower for programming errors.

The above words illustrate the importance of error handling.

The most effective solution is that in programming is, consciously add some mechanisms, so that you can detect yourself at runtime, report before the error is out of control.

The most common error handler is to return a status value for each method (other language, each function) to indicate whether the method is successful and correctly completed the task. When the program that calls this method receives an error status value, it is possible to know that the program is wrong, then take effective measures to avoid problems caused by this error (the easiest way to exit the program, or give a prompt reminder operator) .

First let me introduce a concept: error mode

The so-called error mode, I understand and design the knowledge involved in the design mode should be the same, the error mode is the repeated interrelationship between the error and the potential errors in the program. This concept is not fresh for programming. For extreme programming, RUP, unit test can only solve some of the problems, so we use error mode, an error, as long as we can identify the error mode we can identify One, can you know the cause of this error and correct it.

Interested friends can search for errors, there are many articles introduced. Here are the two error handling methods I want to introduce.

A construction error is abnormal:

Constructing error is abnormal, here is used to discover errors and correct it, first let a code:

INVALIDNAMEEXCEPTION.JAVA

Class InvalidNameException Extends Exception {public invalidNameException () {}}

Program:

Public user validate (string username, string password "." Username.equals ("java")) {} else {throw new invalidnameException ();} // return null;} // Return NULL;

The above code, means that if the username is not java, then throw the invalidNameException () exception, so we can successfully capture this exception and issue a prompt or warning. This constructs a powerful error handling set that can be incorrectly handled inside or outside the program.

Two KEY / VALUES methods.

Find the corresponding error message according to the configuration file, and display it to the user, Struts' error handling method uses this (in fact I said). First look at the following code:

ErrorMessage error (); if (! Username.equals ("java")) {Error.Add ("error.login.username.failed);}

Create an errorMessage to handle the error. If you return empty error, you don't have an error. If you return a non-empty error error, you jump to the Error.jsp page and capture this error. Error information profile:

Login = user login

Error.login.empty_input = landing failed: invalid input!

Error.login.empty_input.desc = Value or other illegal characters may be entered.

Error.login.username.failed = Username Error

Error.login.username.desc = Please refuse back, check if your username is correct!

The above two ways are used together with the log4j, which can achieve the requirements of the error handling, my thoughts are in the system layer, such as the method, throw an exception handling, and processes the user input and related processing in the performance layer. In this way, I used to use it when I wrote the program. Now I will move it again and hope to hear the voice of the reply. I will discuss it. I have a better solution, because my level is limited, the current error handling The design is still based on this.

welcome

Mail

discuss

: Biggie@designac.com

转载请注明原文地址:https://www.9cbs.com/read-25278.html

New Post(0)