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