Exceptions and Assertions Flashcards
ArithmeticException
Thrown when code attempts to divide by 0
Unchecked
ClassCastException
Thrown by the JVM when an attempt is made to cast an object to a subclass of which it is not an instance.
Unchecked
NumberFormatException
Thrown by the program when an attempt is made to convert a string to a numeric type, but the string doesn’t have an appropriate format.
Unchecked
java.text.ParseException
Signals that an error has been reached unexpectedly while parsing.
Checked
Any exception starting with java.io
deals with IO problems
Checked
Any exception starting with java.sql
deals with sql database problems
Checked
java.lang.ArrayStoreException
Trying to store the wrong data type in an array.
Unchecked
java.time.DateTimeException
Receiving an invalid format string for a date.
Unchecked
java.util.MissingResourceException
Trying to access a key or resource bundle that does not exist.
Unchecked
java. lang.IllegalStateException
java. lang. UnsupportedOperationException
Attempting to run an invalid operation in collections and concurrency.
Unchecked
How to create your own checked exception
class myException extends Exception {}
How to create your own unchecked exception
class myException extends RuntimeException {}
What are the three common constructors you can use for your custom exception?
public class myException extends Exception { public myException() { super(); }
public myException(Exception e) { super(e); }
public myException(String message) { super(message); } }
How to give the option to catch multiple exceptions in the same catch statement
Separate the different exception names by |
catch (DateTimeParseException | IOException e)
What is wrong with this catch statement?
catch (FileNotFoundException | IOException e)
FileNotFoundException is a subclass of IOException, and java will not allow this because it is redundant
What is wrong with this?
try { throw new IOException(); } catch(IOException | RuntimeException e) { e = new RuntimeException(); }
When using multi-catch, you cannot reassign the exception variable to something new
Note: you can do that when not using multi-catch
When can you specify to catch a checked exception in a try block?
Only when it is possible for the exception to be thrown by the code, otherwise it won’t compile.
Why would you want to use a try-with-resources statement?
It automatically closes all resources opened in the try clause.
public void newApproach(Path path1, Path path2) throws IOException { try (BufferedReader in = Files.newBufferedReader(path1); BufferedWriter out = Files.newBufferedWriter(path2)) { out.write(in.readLine()); } }
Are catch and finally blocks required when writing a try with resources statement?
No
There is an implicit finally statement to close the resources, but you can also implement your own catch and finally blocks if you want.
Note: The implicit finally block runs before any explicitly coded catch or finally blocks, making the resources no longer available in those blocks!
What is wrong with this?
try (Scanner s = new Scanner(System.in)) { s.nextLine(); } catch(Exception e) { s.nextInt(); } finally{ s.nextInt(); }
s has been closed by the implicit finally block so it is no longer in the scope of the catch and finally block that are coded.
What interface must a class implement in order to be declared in a try-with-resources clause? What is the method that must be implemented?
AutoCloseable
close()
Can close() throw Exception?
Yes, but it is recommended to throw something more specific.
And be sure that if it does, then it is handled in the calling method!
What method does a try-with-resources statement automatically run after the try block?
close()
The method in the interface AutoCloseable
What is a suppressed exception?
When multiple exceptions are thrown before the catch/finally blocks of a try-with-resources statement, there is one primary exception and the rest are stored.
You can loop through the suppressed exceptions like this:
for (Throwable t: e.getSuppressed())
Which exception is thrown first at the end of this try block?
try (JammedTurkeyCage t1 = new JammedTurkeyCage(); JammedTurkeyCage t2 = new JammedTurkeyCage())
The exception that calling close() on t2 produces.
The exceptions stack up.
Assertion
A Boolean expression that you place at a point in your code where you expect something to be true.
Two forms of assert statement
assert boolean_expression;
assert boolean_expression: error_message;
The boolean expression must evaluate to true or false. It can be inside optional paren- thesis. The optional error message is a String
What happens if an assertion is false?
Java throws an AssertionError and the program is ended
How to enable assertions
java -enableassertions Rectangle
You can also use the shortcut -ea flag:
java -ea Rectangle
By default, this enables assertions in all classes but system classes (built in java classes)
How to enable assertions in a specific package/subpackages
java -ea:package_name…
Can also do for specific class
java -ea:package_name.class_name
What is the flag for disabling assertions?
-da
Can use all usual variations like when enabling
How many catch statements can be run?
Only one
Even if that one throws a new exception
Closeable
The interface that was used before AutoCloseable existed.
It has two key requirements:
1) Closeable restricts the type of exception thrown to IOException.
2) Closeable requires implementations to be idempotent (can be called multiple times without any side effects or undesirable behavior on subsequent runs.)