If a method does not handle a possible checked exception, what must the method have?

Checking that thrown objects are caught

A Java compiler checks to make sure that certain thrown objects are caught by your program; if they are not caught, the compiler issues an error message and refuses to compile the program. For example, consider this method main:

public static void main(String[] args) {
    throw new Exception();
}

(This method is useless ---except to illustrate our point.) Upon attempting to compile this method, the compiler issues this error message:

Error: Exception java.lang.Exception must be caught or
it must be declared in the throws clause of this method.

You can get rid of this message by using a try-statement that catches the thrown object (again, this is a useless program except to illustrate our point). But don't do this simply to have an exception ignored! If your program cannot reasonably handle a thrown exception, don't use the following trick to get rid of it.

public static void main(String[]) {
   try {
      throw new Exception();
   } catch {Exception ex) {
      ...
   }
}

Instead, use a throws-clause, as explained below. But before we explain the throws-clause, we talk about checked and unchecked objects.

Checked and unchecked objects

Checking that thrown objects are caught is a good idea, for it forces the programmer to think carefully about how thrown objects should be handled. But the Java compiler does not check ALL thrown objects in this manner. Checking all possible thrown objects would be awkward and cumbersome, for there are many possibilities. For example, there are many possible RuntimeExceptions like divide-by-0, index-out-of-bounds, and null-pointer-exception.

Java checks all of throwable classes EXCEPT:

  1. Thrown objects of class Error and its subclasses
  2. Thrown objects of class RuntimeException and its subclasses

The throws clause

If a method does not handle a possible checked exception, what must the method have?
A method might not WANT to catch a thrown object. Instead, it might want to have it thrown further to the calling method. To eliminate the error mentioned at the beginning of this webpage and have the thrown object thrown further, put a throws clause on the method header. Here's the modified script.

The throws clause has the form

throws <class-name>, ..., <class-name>

where each class-name is Throwable or one of its subclasses. Placing a throws clause on a method relieves the method of the responsibility of catching objects of the named classes and places the responsiblity on any method that calls this one.

Here's a suggestion. Don't worry about putting in throws clauses. Put them out of your mind. But, whenever Javatells you that an exception must be caught, put in the throws clause.

©This material is from the CD ProgramLive by David Gries and Paul Gries

The previous section showed how to write an exception handler for the writeList method in the ListOfNumbers class. Sometimes, it's appropriate for code to catch exceptions that can occur within it. In other cases, however, it's better to let a method further up the call stack handle the exception. For example, if you were providing the ListOfNumbers class as part of a package of classes, you probably couldn't anticipate the needs of all the users of your package. In this case, it's better to not catch the exception and to allow a method further up the call stack to handle it.

If the writeList method doesn't catch the checked exceptions that can occur within it, the writeList method must specify that it can throw these exceptions. Let's modify the original writeList method to specify the exceptions it can throw instead of catching them. To remind you, here's the original version of the writeList method that won't compile.

public void writeList() {
    PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt"));
    for (int i = 0; i < SIZE; i++) {
        out.println("Value at: " + i + " = " + list.get(i));
    }
    out.close();
}

To specify that writeList can throw two exceptions, add a throws clause to the method declaration for the writeList method. The throws clause comprises the throws keyword followed by a comma-separated list of all the exceptions thrown by that method. The clause goes after the method name and argument list and before the brace that defines the scope of the method; here's an example.

public void writeList() throws IOException, IndexOutOfBoundsException {

Remember that IndexOutOfBoundsException is an unchecked exception; including it in the throws clause is not mandatory. You could just write the following.

public void writeList() throws IOException {

What happens if a program does not handle an unchecked?

If the program does not handle an unchecked exception: B. the program is halted and the default exception handler handles the exception.

When you write a method that throws a checked exception you must?

If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using the throws keyword. In checked exception, there are two types: fully checked and partially checked exceptions.

When an exception is thrown by a method that is executing?

8) When an exception is thrown by a method that is executing under several layers of method calls, a stack trace indicates the method executing when an exception occurred and all of the methods that were called in order to execute that method.

What can a method do with a checked exception quizlet?

Two things a method must do with a checked exception. The method throws the exception to its caller. When an exception is thrown in a try block and there is no matching catch block, this happens. Stream type which provides input from a disk file.