Please send questions to
st10@humboldt.edu .
/* ExeptnPlay1.java 1.0 */
/* by Sharon Tuttle */
/* last modified: 3-6-03 */
/* */
/* playing with Java exceptions in a */
/* testing-function kind of way... */
public class ExceptnPlay1 extends JPFalt
{
public static void main(String[] args)
{
new ExceptnPlay1();
}
/** Playing around with Java exceptions */
/*--------------------------------------------------------------------
Playing with Java arrays and the exceptions they can throw
--------------------------------------------------------------------*/
void arraysAndExceptions()
{
testHeader("playing with arrays and exceptions");
println("\nSet up an array of Strings");
// this declares the array, but it is
// a reference to null --- I haven't given anything
// for it to refer TO yet;
String[] phrase;
// TEST #1: ...SO, if I try to give a reference to an element,
// that's a bad thing; won't even compile...?
//phrase[0] = "Howdy, folks!";
// let's now specify that this is an array of 10 String's.
phrase = new String[10];
// NOW I can do this:
phrase[0] = "Howdy, folks!";
expected("Howdy, folks!");
actual( phrase[0] );
// TEST #2: What if I try to use a NEGATIVE array index?
// (now you'll see what a thrown exception does in BlueJ;
// note that, using javac at the command line, you should
// see an exception message to the screen, and in some other
// Java IDE's, you'll get the message on the so-called Java
// console)
println("\ntry to insert using array index -1:");
//phrase[-1] = "Please don't put me in...";
// TEST #3: let's CATCH this exception, instead;
try
{
phrase[-1] = "Please don't put me in...";
println("NEVER PRINTED IF exception occurs!");
}
catch (ArrayIndexOutOfBoundsException ex)
{
// print the toString depiction of this particular
// Exception subclass object...
println("Exception occurred: " + ex);
}
finally // ALWAYS done, note!
{
println("ArrayIndexOutOfBoundsException should have occurred.");
}
// TEST #4: is it any different if I use too LARGE of an index?
println("\ntry to insert into index equal to array length");
//phrase[phrase.length] = "One position too far";
// yes, you can catch it if you are not sure what kinds of
// exceptions might be thrown --- all are also of type
// Exception, too, aren't they?
try
{
phrase[phrase.length] = "One position too far";
}
catch (Exception ex)
{
println("Exception occurred: " + ex);
}
finally
{
println("Did we just see what exception occurred?");
}
// TEST #5: what happens if I try to insert a value
// of an inappropriate type into the array?
// (ah --- compiler catches it! but not always...?)
//phrase[1] = 13;
// TEST #6: sometimes array storage checks are done at
// run-time, not compile time;
//-------------------------------------------------
// EXAMPLE ADAPTED FROM "Java Precisely", Sestoft, p. 11
//-------------------------------------------------
println("\nArrayStoreException example, " +
"from \"Java Precisely\", p. 11:");
// Number is an abstract class with subclasses such as
// the wrapper classes for Integer, Double, etc.!
//
// ... so, it is okay for a Number-array to contain 10
// Integer instances...
Number[] numArray = new Integer[10];
// create a Double object in myDub
Double myDub = new Double(3.14);
// create an Integer object in myInt
Integer myInt = new Integer(117);
// note that myNum, being of type Number, can
// hold instances of subclasses of Number, since
// they are considered to be of the type Number, too
Number myNum = myInt;
numArray[0] = myInt; // OK --- Integer IS a subtype of Integer
numArray[1] = myNum; // OK --- type is Number, but ALSO Integer
//numArray[2] = myDub; // not OK --- Double is NOT a subtype of
// Integer
// let's see what exception IS thrown:
try
{
numArray[2] = myDub;
println("for fun: this is NOT PRINTED!");
}
catch (Exception ex)
{
println("Exception thrown: " + ex);
}
finally
{
println("did we just see what Exception was thrown?");
}
println("what is in numArray now? (only [0], [1] should be filled)");
for (int i=0; i < numArray.length; i++)
{
if (numArray[i] != null)
{
println("numArray[" + i + "]: " + numArray[i]);
}
else
{
println("numArray[" + i + "] is null");
}
}
}
// I made a separate new class, BadGradeException. Its
// constructor expects the out-of-range numeric grade
// as its argument.
void badGradeExceptionText() throws BadGradeException
{
Integer[] grades = new Integer[15];
int base = -10;
for (int i=0; i < grades.length; i++)
{
if ((base < 0) || (base > 100))
{
throw new BadGradeException(new Integer(base));
}
grades[i] = new Integer(base);
// increment base by 10
base += 10;
}
for (int i=0; i < grades.length; i++)
{
if (grades[i] != null)
{
println("grades[" + i + "]: " + grades[i]);
}
else
{
println("grades[" + i + "] is null");
}
}
}
}