Exception Handling and Text I/O 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
Describe the exception classes hierarchy
Throwable is the root exception class (it's superclass is Object). Exception and Error are Throwable's subclasses. Error contains subclasses that describe system errors. Exception contains subclasses that describe errors caused by your program and by external circumstances. One of Exception's subclasses is RuntimeException. RuntimeException contains subclasses that describe programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.
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.
Which exception classes are checked, and which are unchecked?
RuntimeException and Error, and their subclasses are unchecked exceptions. All other exceptions classes are checked.
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 { FileInputStream f = new FileInputStream(name); }
What is important in regards to the order of catch blocks?
Catch blocks that catch a superclass exception must appear after a catch block for a subclass exception.
When should you use exceptions?
A method should throw an exception if the error needs to be handled by its caller.
If you can handle the exception in the method where it occurs, there is no need to throw or use exceptions.
In general, common eceptions that may occur in multiple classes in a project are candidates for exception classes. Simple errors that may occur in individual methods are best handled without throwing exceptions – by using if statements to check for errors.
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 ex;
}
The statement “throw 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.
How do you create your own exception class?
By extending Exception or one of its subclasses. Example: public class InvalidRaidusException extends Exception { private double radius;
/** Construct an exception */ public InvalidRadiusException(double radius) { super("Invalid radius " + radius); this.radius = radius; } }
You can then create a setRadius method that looks like this:
public void setRadius(double newRadius) throws InvalidRadiusException {
if (newRadius >= 0)
radius = newRadius;
else
throw new InvalidRadiusException(newRadius);
}
What can you do with the File class?
The File class contains the methods for obtaining the properties of a file/directory and for renaming and deleting a file/directory. It does not contain the methods for creating a file or for writing/reading data to/from a file.
Why shouldn’t you use absolute file names in your program?
Because file path syntax is different for different platforms.
How do you create a file and write to it?
You use the java.io.PrintWriter class.
PrintWriter output = new PrinterWriter(fileName);
to create the file fileName (which can be a File object).
output.println(“ost”);
output.print(“ost”);
to write to the file.
Remember to close the file when finished, with:
output.close();
Note that checked exceptions may be thrown.
How do you read from a file?
You use the java.util.Scanner class.
To read from a file, create a Scanner for a file, as follows:
Scanner input = new Scanner(new File(filename));
or if File object is already created:
Scanner input = new Scanner(filename);
Note that checked exceptions may be thrown.