Man legt eine Variable alleine für Fehler dieses Programmteils an und kann dann bei Bedarf diese überprüfen, um die Ursache des Fehlers zu finden.
Code: Alles auswählen
#define NO_FAULT 0
#define NULLDIV 1
#define INT_DIV_BECOMES_SMALLER_NULL 2
int last_math_fault = 0;
void math_fault(int new_error)
{
last_math_fault = new_error;
}
int Get_last_math_fault (void)
{
return last_math_fault;
}
int div(int a, int b)
{
if (b==0) {
math_fault(NULLDIV);
return 0;
}
if (b > a) {
math_fault(INT_DIV_BECOMES_SMALLER_NULL);
return 0;
}
math_fault (NO_FAULT);
return a/b;
}
int main (void)
{
int return_value = div(5,0);
if (Get_last_math_fault != NO_FAULT) {
printf("Da war was...");
if (Get_last_math_fault == NULLDIV) {
printf("Division durch 0!");
}
}
return return_value;
}
Natürlich kann man das auch als eine Art "Stack" betreiben
Oder als Queue
Dann können mehrere Fehler zeitlich richtig gespeichert werden.
Oder, wenn mans ganz auf die Palme treibt auch als verkettete Liste^^
MfG
Daniel
P.S: Ja, ich weiß, das Beispiel ist etwas...naja...es ist recht unnötig^^