Java Reference
In-Depth Information
throw
throw new
new NullPointerException ( "Force Exception" );
}
Object o = new
new Object ();
al . add ( o . toString ());
} catch
catch ( NullPointerException npe ) {
// Continue to get next string
}
}
return
return al ;
}
public
public ArrayList < String > testDefensiveProgramming () {
ArrayList < String > al = new
new ArrayList <>();
for
for ( int
int i = 0 ; i < numTestLoops ; i ++) {
Object o = null
null ;
iif (( i % exceptionFactor ) != 0 ) {
o = new
new Object ();
}
iif ( o != null
null ) {
al . add ( o . toString ());
}
}
return
return al ;
}
Each method here returns an array of arbitrary strings from newly created objects. The size
of that array will vary, based on the desired number of exceptions to be thrown.
Table 12-5 shows the time to complete each method for 100,000 iterations given the worst
caseā€”an exceptionFactor of 1 (each iteration generates an exception, and the result is an
empty list). The example code here is either shallow (meaning that the method in question is
called when there are only 3 classes on the stack), or deep (meaning that the method in ques-
tion is called when there are 100 classes on the stack).
Table 12-5. Time to process exceptions at 100%
Method
Shallow time Deep time
Code exception
381 ms
10,673 ms
System exception
15 ms
15 ms
Defensive programming 2 ms
2 ms
Search WWH ::




Custom Search