Chapter 14 - Handling errors Flashcards
Why is notifying the user via a GUI not always the preferred method for
error reporting?
Notifying the user via a GUI assumes that there will always be a human user present to see the message, which is not always the case. Additionally, the user may not be in a position to do anything about the problem, such as in the case of an ATM machine.
this is usually used for errors that the programmer may have control over.
What is the purpose of the
Exception subclass?
What are the two methods provided by the
Files class
to read an entire file?
The two methods provided by this class to read an entire file are lines() and readAllLines().
this is program code that protects statements that may throw an exception.
If an exception is thrown, it can be handled by either creating a report or attempting to recover.
What is an
exception handler?
What are unchecked exceptions in Java?
these are exceptions that would never fail in normal operation, usually indicating some kind of programming error.
These exceptions are not required to be declared in a method’s throws clause, which means that the programmer is not obligated to catch or handle them explicitly.
Examples of unchecked exceptions include calling a method on a null object.
Why is it often harder to find logical errors in a program?
Logical errors cannot be spotted by the compiler unlike syntactical errors and will only show up at some point during runtime.
What are the
natural units to use when reading from files?
The natural units to use when performing this are characters and lines.
This is important in coding because it allows code to warn other code, a human operator, or a programmer that an error has arisen.
This allows the error to be corrected or handled properly, preventing potential issues or vulnerabilities.
Why is
error reporting
important in coding?
this is important in file-based input and output because the programmer has little to no control over the external environment on which the program is run, which means that errors can occur due to various causes, such as required files being deleted or overwritten or corrupted.
Why is
error recovery
important in file-based input and output?
What are unchecked exceptions?
these are exceptions that the compiler enforces few rules on their use, and they are not checked by the compiler.
What are the two subclasses of
Throwable?
subclasses of this include Error and Exception.
How can a
BufferedReader
be created?
this can be created via a static method of Files named NewBufferedReader().
What are the two main categories of classes in the java.io package?
The two main categories of classes in the java.io package are:
- Those that deal with text files (.txt, .html), which are subclasses of the abstract classes Reader and Writer.
- Those that deal with binary files (.png, .exe), which are subclasses of the abstract classes InputStream and OutputStream.
the syntax that allows this is to use the throws keyword in the header followed by comma separarted exceptions
example:
Public void process() throws EOFException, FileNotFoundException
what is the syntax that allows a method or constructor to throw multiple exceptions
What is an example of a
runtime error
that could occur if the code does not implement defensive programming?
An example of this is a null pointer exception, which could occur when we try to call a method on a null object.
This could happen, for example, when we are given an incorrect key and try to access a collection object with that key, such as when removing objects.
What are the 4 steps for saving an object using object serialization? can you write the code?
To accomplish this:
- Ensure the class of the object we wish to save implements the Serializable interface.
- Create an instance of the ObjectOutputStream class, passing in an OutputStream object.
- Call the writeObject() method of the ObjectOutputStream, passing in the object you want to save.
- Close the ObjectOutputStream.
Example:
public void saveToFile(Person person, String destinationFile) { try { // 2. Create an instance of the ObjectOutputStream class, passing in an OutputStream object. FileOutputStream fileOut = new FileOutputStream(destinationFile); ObjectOutputStream out = new ObjectOutputStream(fileOut); // 3. Call the writeObject() method of the ObjectOutputStream, passing in the object you want to save. out.writeObject(person); // 4. Close the ObjectOutputStream. out.close(); } catch (IOException i) { i.printStackTrace(); } }
What is the difference between the next() and nextLine() methods in the Scanner class?
The next() method finds and returns the next complete token from the scanner, while the nextLine() method reads in the next line of text as a string.
Note:
By default, each token is defined as being separated by whitespace, although other separators can be defined.
these include Checked exceptions and unchecked exceptions.
What are the two categories of exception classes in Java?
these may be found in the java.nio.charset package.
What package contains Charsets?
What are the classes in the java.io package that deal with binary files?
The classes in the java.io package that deal with binary files are stream handlers, which are subclasses of the abstract classes InputStream and OutputStream.
for a resource to be able to use the “try with resource” statement what must it implement
in order for a resource to make use of this it must implement the AutoCloseable interface that is defined in the java.lang package.
this is an exception that occurs when we try to access an index that is out of bounds of the operation.
What is an
IndexOutOfBoundsException?
This is a process in programming that aims to prevent errors from occurring as much as possible, in order to avoid the program crashing at runtime and to avoid creating clunky or messy error recovery processes in the client.
What is
error avoidance?
What are the two variant syntax of an assert statement?
syntax includes:
assert booleanExpression;
or
assert booleanExpression : errorMessage;
The second form allows you to provide a custom error message that will be displayed if the assertion fails.