Consider the following class:
class Xyz {
public int count;
public void numZero (int[] x) {
// Effects: if x == null throw NullPointerException
// else return the number of occurrences of 0 in x
int count = 0;
for (int i = 1; i < x.length; i++) //we have a bug here
{
if (x[i] == 0)
{
count++;
}
}
this.count = count;
}
}
I'm trying to wrap my head about what Fault
, Error
and Failure
really mean.
Fault
From what I've come to understand, a Fault
in this context would be a flaw in the code's written logic.
So in this case the Fault
would be the fact that the code instructs the computer to start iterating over all elements of v
with a start index of 1 instead of the expected 0.
Error
When running the above method, we always get an Error
but in once instance (when v.length == 0
), as what we really want is to iterate over all elements of x
, but since we're starting with i = 1
, that is not really happening.
With an empty vector as input, as we don't enter the for loop
, so our incorrect code isn't run, meaning that the Error doesn't happen, and everything happens as should in theory.
Failure
Since our code has a Fault
that in execution-time will almost always manifest in a Error
, we only have a Failure
when we effectively see the incorrect output.
Assuming that an Error
effectively happened in my program, we only have a Failure if it is in some way visible to the outside world. That is, had I private int count
; instead of public int count;
I'd never ever have an Error in my class (of course it'd be the most useless class ever!). Is this right?
Is everything I said correct or am I erring in something?
Thanks