Unusual concept
- the program may generate an exception during the run
- Exception (Exception) Bug difference between the anomalies are to be expected runtime execution branch Bug are bugs in the program, is not expected to be operational mode
Exception (Exception) and Comparative Bug
- abnormality
is generated in addition to the runtime zero
need to open the external file does not exist
when the array access violation
-Bug
using wild pointers
not released after the end of the stack array using
array processing can not select a sort of length 0
C language classical approach: the else ... ... IF
void FUNC (...)
{
IF ( determined whether an abnormality )
{ normal code logic ; } the else { abnormality code logic ; } }
#include <iostream>
#include <string>
using namespace std;
double divide(double a, double b, int* valid)
{
const double delta = 0.000000000000001;
double ret = 0;
if( !((-delta < b) && (b < delta)) )
{
ret = a / b;
*valid = 1;
}
else
{
*valid = 0;
}
return ret;
}
int main(int argc, char *argv[])
{
int valid = 0;
double r = divide(1, 0, &valid);
if( valid )
{
cout << "r = " << r << endl;
}
else
{
cout << "Divided by zero..." << endl;
}
return 0;
}
缺陷
-divide函数有3个参数,难以理解其用法
-divide函数调用后必须判断valid代表的结果
当valid为true时,运算结果正常
当valid为false时,运算过程出现异常