Checking that thrown objects are caughtA 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: Show
(This method is useless ---except to illustrate our point.) Upon attempting to compile this method, the compiler issues this error message:
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.
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 objectsChecking 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:
The throws clauseThe throws clause has the form
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 If the 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
public void writeList() throws IOException, IndexOutOfBoundsException { Remember that 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.
|