public class CatchExceptions {
public static void main(String [] args) {
int [] a = {3, 1, 0, 4, 6};
int [] b = {2, 0, 4, 8};
for (int i = 0; i < a.length; ++i) {
try {
System.out.println(a[i]/b[i]);
} catch (Exception e) {
System.out.println(e);
}
}
System.out.println("Done");
}
}
Question 2
Suppose we put the try/catch block around the entire loop instead of
just the print statement (see
below). What would happen? Run the program and explain the change in
the program's behavior.
public class CatchExceptions {
public static void main(String [] args) {
int [] a = {3, 1, 0, 4, 6};
int [] b = {2, 0, 4, 8};
try {
for (int i = 0; i < a.length; ++i) {
System.out.println(a[i]/b[i]);
}
} catch (Exception e) {
System.out.println(e);
}
System.out.println("Done");
}
}
Question 3
Go back to the version of this program given in Question 1 and change
the statement catch (Exception e)
to catch
(ArithmeticException e)
. What do you think will happen? Run the
program, explain the results.
Question 4
Now change the same statement to catch (IndexOutOfBoundsException e)
. Explain the results.
Question 5
What happens if I replace
System.out.println(e);
by each of the following:
public class ThrowExceptions {
public static void main(String [] args) throws Exception {
int [] a = {3, 2, 1, 4, 5};
for (int i = 0; i < a.length; ++i) {
System.out.println(a[i]);
if (a[i] % 2 == 0) {
throw new Exception("I hate even numbers");
}
}
System.out.println("Done");
}
}
Question 2
What happens if you remove throws Exception
from the
declaration of main?
Question 3 Consider the following variation of the same program:
public class ThrowExceptions {
public static void main(String [] args) {
int [] a = {3, 2, 1, 4, 5};
for (int i = 0; i < a.length; ++i) {
System.out.println(a[i]);
try {
if (a[i] % 2 == 0) {
throw new Exception("I hate even numbers");
}
} catch (Exception e) {
System.out.print("Hey, I caught an exception! ");
System.out.println("It says: " + e.getMessage());
}
}
System.out.println("Done");
}
}
What will happen if you run it? Explain the results.
Question 4Now change the catch block to:
public class ThrowExceptions {
public static void main(String [] args) throws Exception {
int [] a = {3, 2, 1, 4, 5};
for (int i = 0; i < a.length; ++i) {
System.out.println(a[i]);
try {
if (a[i] % 2 == 0) {
throw new Exception("I hate even numbers");
}
} catch (Exception e) {
System.out.print("Hey, I caught an exception! ");
System.out.println("It says: " + e.getMessage());
if (i > 2) throw e;
}
}
System.out.println("Done");
}
}
Explain the results.
IHateEvenNumbersException.java
write a
class IHateEvenNumbersException
. The class must
extend the class Exception. Write a constructor that calls the
constructor of the superclass with one parameter: a string "I hate
even numbers!!".
Replace the line
throw new Exception("I hate even numbers");
with the line
throw new IHateEvenNumbersException();
Test the program.
Question 2
Change the line catch (Exception e)
to catch only
IHateEvenNumbersException. Test your program to make sure that other
kinds of exceptions go uncaught (you need to write code that gives
some exceptions, such as ArithmeticException or
NullPointerException). Submit your program (all the files you have
written or modified) to the Wiki.
public class Typecasting {
public static void main(String [] args) {
Object [] stuff = {"apples", new StringBuffer("oranges"),
"pineapples", new StringBuffer("grapes"),
"kiwi"};
Object [] morestuff = new Object[stuff.length];
for (int i = 0; i < stuff.length; ++i) {
String s = (String) stuff[i];
morestuff[i] = s;
}
}
}
Rewrite the program so that it prints out an error message for each
improperly typecasted object, but continues execution. Do not use
instanceof
, catch the typecasting exception
instead. Print out the array morestuff after the loop. Explain the
results. Submit the file Typecasting.java.