6.170 / Spring 2001 / Lecture 8 Card Answers

Handout L8-QA

Exceptions


Question: How can a method declare that it is throwing an exception (what is the syntax)?
Answer: A method declares that it might throw an exception using a throws clause.

Ex.

pubic void open_file() throws IOException {

The actual throw is accomplished using a throw statement.

Ex.

throw new IOException();

Question: What is a good reference that describes all the different exception types in Java?
Answer: Here are a few such references:

A tutorial on exceptions is also available at Exception Tutorial

Question: Why are checked exceptions preferable to unchecked (runtime) exceptions.
Answer: The answer to this is in Runtime Exceptions - The Controversy

Question: Can a method throw multiple exceptions? If so, how?
Answer: A method can throw more than one kind of exception. For example, suppose you've defined two exception classes, MyException1, and MyException2. These would be declared in your method using the throws clause:

public void myfunc(int arg) throws MyException1, MyException2 {

The method myfunc could then have throw statements for MyException1, and MyException2. In fact, if MyException1 or MyException2 have subclasses, these could be thrown as well. For example, a method that declares in its throws clause that it throws IndexOutOfBoundsException can throw IndexOutOfBoundsException, ArrayIndexOutOfBoundsException, and StringIndexOutOfBoundsException, because the latter two are subclasses of the first.

Question: How do you define a new kind of exception?
Answer: You define a new kind of exception by extending the class Exception (you define a subclass of Exception). A simple example is

public class MyException extends Exception {
  public MyException() { super(); }
}

A more sophisticated exception might take constructor arguments and store the values in fields. This would allow for storing detailed information about the exception that is then available in the catch statement when the exception is caught.

Question: What is an example of code that catches and exception and then throws it again to be caught at a higher level?
Answer: The following example shows a try statement with a catch that catches an exception, and then throws it again.

try{
  ...
}
catch (MyException e) {
  System.out.println("MyException caught here initially");
throw e;
}

Question: If you catch and re-throw an exception, where will the error be shown? On the original throw, or the second throw? Where will the stack trace show it?
Answer: The way that errors are shown depends on the code in the catch blocks, so it is really up to the developer of the code. One common technique is to print information about the exception (using System.out.println). The catch block code typically accesses this information from the exception itself; the exception is given this information when it is constructed. Thus, the catch of the first throw may or may not print information about the exception. Similarly, the catch for the second throw may or may not print information. There are some circumstances where the exception can be handled without bothering the user.

Stack traces do not show exceptions; they show stack frames. Before an exception is thrown, the stack trace shows the procedure in which the exception is about to be thrown at the top of the stack. After the exception is thrown, the procedure that has caught the exception is at the top of the stack.

Question: What are the runtime costs of exceptions? Can catch blocks slow programs?
Answer: Runtime costs of exceptions depend on the Java implementation. In a good implementation, during normal execution where throws aren't made, catch blocks should not slow things down (because the code in them is not invoked). When throws are made, there is some overhead due to the "unwind protect" in the try blocks; the try blocks need to check whether a thrown exception is caught by the try block's corresponding catch statements, or whether the exception should be propagated upwards.


Back to Index Card Answers.
Back to the 6.170 home page.
For problems or questions regarding this page, contact: 6.170-webmaster@mit.edu.