Don't rely on the exception in your code

xiaoxiao2021-03-06  109

Because exception greatly reduces performance, you should not use them as a way to control the normal procedure. If it is possible to detect a state in which the code may result in anomalous, do this. Do not capture an exception itself before processing the state. Common solutions include: Checking NULL, assigning a string that will be analyzed as a numeric value, or check a particular value before applying mathematical operations. The following example demonstrates the code that can cause an abnormal code and whether there is a code that exists in some state. Both produce the same result.

[C #]

// consider changing this ...

Try {

Result = 100 / NUM;

}

Catch (Exception E) {

Result = 0;

}

// ... to this.if (NUM! = 0) Result = 100 / num; elseresult = 0; [Visual Basic] 'Consider Changing this ... try ... try ... try ... try ... try ...

// ... to this.if NOT (NUM = 0) Result = 100 / NUMELSERESULT = 0END IF

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

New Post(0)