- Question: Is the following code legal?Answer: Yes, it's legal — and very useful A
try { } finally { }trystatement does not have to have acatchblock if it has afinallyblock. If the code in thetrystatement has multiple exit points and no associatedcatchclauses, the code in thefinallyblock is executed no matter how thetryblock is exited. Thus it makes sense to provide afinallyblock whenever there is code that must always be executed. This include resource recovery code, such as the code to close I/O streams. - Question: What exception types can be caught by the following handler?What is wrong with using this type of exception handler?Answer: This handler catches exceptions of type
catch (Exception e) { }Exception; therefore, it catches any exception. This can be a poor implementation because you are losing valuable information about the type of exception being thrown and making your code less efficient. As a result, your program may be forced to determine the type of exception before it can decide on the best recovery strategy. - Question: Is there anything wrong with this exception handler as written? Will this code compile?Answer: This first handler catches exceptions of type
try { } catch (Exception e) { } catch (ArithmeticException a) { }Exception; therefore, it catches any exception, includingArithmeticException. The second handler could never be reached. This code will not compile. - Question: Match each situation in the first list with an item in the second list.
int[] A;
A[0] = 0;- The JVM starts running your program, but the JVM can't find the Java platform classes. (The Java platform classes reside in
classes.ziporrt.jar.) - A program is reading a stream and reaches the
end of streammarker. - Before closing the stream and after reaching the
end of streammarker, a program tries to read the stream again.
- __error
- __checked exception
- __compile error
- __no exception
Answer:- 3 (compile error). The array is not initialized and will not compile.
- 1 (error).
- 4 (no exception). When you read a stream, you expect there to be an end of stream marker. You should use exceptions to catch unexpected behavior in your program.
- 2 (checked exception).
Exercises
- Exercise: Add a
readListmethod toListOfNumbers.java. This method should read inintvalues from a file, print each value, and append them to the end of the vector. You should catch all appropriate errors. You will also need a text file containing numbers to read in.public class ListOfNumbers { private List<Integer> list; private static final int SIZE = 10; public ListOfNumbers () { list = new ArrayList<Integer>(SIZE); for (int i = 0; i < SIZE; i++) list.add(new Integer(i)); } public void writeList() { PrintWriter out = null; try { System.out.println("Entering try statement"); out = new PrintWriter(new FileWriter("OutFile.txt")); for (int i = 0; i < SIZE; i++) out.println("Value at: " + i + " = " + list.get(i)); } catch (IndexOutOfBoundsException e) { System.err.println("Caught IndexOutOfBoundsException: " + e.getMessage()); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); } finally { if (out != null) { System.out.println("Closing PrintWriter"); out.close(); } else { System.out.println("PrintWriter not open"); } } }} Answer: Seepublic class ListOfNumbers2 {.ListOfNumbers2.java
private Vector<Integer> victor; private static final int SIZE = 10; public ListOfNumbers2() { victor = new Vector<Integer>(SIZE); for (int i = 0; i < SIZE; i++) victor.addElement(new Integer(i)); this.readList("infile.txt"); this.writeList(); } public void readList(String fileName) { String line = null; try { RandomAccessFile raf = new RandomAccessFile(fileName, "r"); while ((line = raf.readLine()) != null) { Integer i = new Integer(Integer.parseInt(line)); System.out.println(i); victor.addElement(i); } } catch(FileNotFoundException fnf) { System.err.println("File: " + fileName + " not found."); } catch (IOException io) { System.err.println(io.toString()); } } public void writeList() { PrintWriter out = null; try { out = new PrintWriter(new FileWriter("outfile.txt")); for (int i = 0; i < victor.size(); i++) out.println("Value at: " + i + " = " + victor.elementAt(i)); } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage()); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); } finally { if (out != null) { System.out.println("Closing PrintWriter"); out.close(); } else { System.out.println("PrintWriter not open"); } } } public static void main(String[] args) { new ListOfNumbers2(); } } - Exercise: Modify the following
catmethod so that it will compile:Answer: The code to catch exceptions is shown in bold:public static void cat(File file) { RandomAccessFile input = null; String line = null; try { input = new RandomAccessFile(file, "r"); while ((line = input.readLine()) != null) { System.out.println(line); } return; } finally { if (input != null) { input.close(); } } }
public static void cat(File file) { RandomAccessFile input = null; String line = null; try { input = new RandomAccessFile(file, "r"); while ((line = input.readLine()) != null) { System.out.println(line); } return; } catch(FileNotFoundException fnf) { System.err.format("File: %s not found%n", file); } catch(IOException e) { System.err.println(e.toString()); } finally { if (input != null) { try { input.close(); } catch(IOException io) { } } } }
No comments:
Post a Comment