|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
- Question: Is the following code legal?
Answer: Yes, it's legal. Atry { ... } finally { ... }trystatement does not have to have acatchstatement if it has afinallystatement. If the code in thetrystatement has multiple exit points and no associatedcatchclauses, the code in thefinallystatement is executed no matter how thetryblock is exited.
- Question: What exception types can be caught by the following handler?
What is wrong with using this type of exception handler?catch (Exception e) { ... }Answer: This handler catches exceptions of type
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, the runtime system is forced to determine the type of exception before it can decide on the best recovery strategy.
- Question: What exceptions can be caught by the following handler?
Is there anything wrong with this exception handler as written? Will this code compile?... } catch (Exception e) { ... } catch (ArithmeticException a) { ... }Answer: This first handler catches exceptions of type
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 column with an item in the second column.
int[] A;
A[0] = 0;
- The Java VM starts running your program, but the VM 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 stream marker.
- Before closing the stream and after reaching the end of stream marker, a program tries to read the stream again.
- error
- checked exception
- runtime exception
- no exception
Answer:
- 3 (runtime exception).
- 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).
- Exercise: Add a
readListmethod toListOfNumbers.java. This method should read in
intvalues 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.Answer: See
ListOfNumbers2.java.
- Exercise: Modify the following
catmethod so that it will compile:public static void cat(File named) { RandomAccessFile input = null; String line = null; try { input = new RandomAccessFile(named, "r"); while ((line = input.readLine()) != null) { System.out.println(line); } return; } finally { if (input != null) { input.close(); } } }Answer: The code to catch exceptions is shown in red:
public static void cat(File named) { RandomAccessFile input = null; String line = null; try { input = new RandomAccessFile(named, "r"); while ((line = input.readLine()) != null) { System.out.println(line); } return; } catch(FileNotFoundException fnf) { System.err.println("File: " + named + " not found."); } catch(Exception e) { System.err.println(e.toString()); } finally { if (input != null) { try { input.close(); } catch(IOException io) { } } } }
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.