Exception Handling Flashcards
How do the ‘try’, ‘catch’, and ‘finally’ statements interact?
Example:
try {
Code to run;
Statement or method that may throw an exception;
More code to run if no exception;
}
catch (exceptionType ex) {
Code to process the exception;
}
finally {
This code always runs, whether or not an exception is thrown in the try statement.
}
What are exceptions?
They are objects created from the exception class
What is a checked exception?
A checked exception is an exception that the compiler forces the programmer to deal with, either with a try-catch block or declare it in the method header.
How can you deal with checked exceptions?
You can either catch it in an ordinary try statement, like this:
public static void openFile(String name)
{
try
{
FileInputStream f =
new FileInputStream(name);
}
catch (FileNotFoundException e)
{
System.out.println(“File not found.”);
}
}
or, if you don’t want to deal with the exception in the method that creates the exception, you can throw it “up the line”, back to the caller like this:
public static void openFile(String name)
throws FileNotFoundException
{
// code block
}
What is meant by “rethrowing exceptions”?
Java allows an exception handler to rethrow the exception if the handler cannot process the exception or simply wants to let its caller be notified of the exception. The syntax looks like this:
try {
statements;
}
catch (TheException ex) {
perform operations before exits;
throw new ex;
}
The statement “throw new ex;” rethrows the exception back to the caller so that other handlers in the caller get a chance to process the exception.
When should you create your own exception class?
You should use the built-in exception classes whenever possible, but if you run into a problem that cannot be adequately described by the exception classes, you can create your own exception class, derived from Exception or from a subclass of Exception.
An instance of ____ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully.
Errors
An instance of ___ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program.
Exceptions
An instance of ___ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.
RuntimeException
What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
int[] list = new int[5];
System.out.println(list[5]);
}
}
ArrayIndexOutOfBoundsException
Which of the following statements are true?
A. You use the keyword throws to declare exceptions in the method heading.
B. A method may declare to throw multiple exceptions.
C. To throw an exception, use the key word throw.
D. If a checked exception occurs in a method, it must be either caught or declared to be thrown from the method.
All are true.
What is wrong in the following program?
class Test {
public static void main (String[] args) {
try {
System.out.println(“Welcome to Java”);
}
}
}
You cannot have a try block without a catch block or a finally block.
what does the throws keyword (throw clause) do when placed after the method parameters closing parenthesis and before the opening curly brace for the body
it allows a calling method to actually deal with the exception later, and not to deal with it directly in the method where the exception occurred.
can a throws clause have multiple exceptions?
yes, multiple comma separated exceptions can be included in a throws clause.
what does throws Exception do?
It is the same as declaring that any class that extends from the Exception class can be thrown. that way you don’t have to list them all out