exxxcepppppppption Flashcards
What does Exception and Error extend?
Class called Throwable
What is the sub class of Throwable?
Exception and Error
What is the sub class of Exception
RunTimeException
What are runtime exception? They are also known as?
unchecked exceptions.
Exceptions tend to be unexpected but not necessarily fatal
What is checked exception?
Checked exceptions tend to be more anticipated—for example, trying
to read a fi le that doesn’t exist.
java.lang.RuntimeException extends what?
java.lang.Exception
What is throw?
tells Java that you want to
throw an Exception
What is throws?
throws simply declares that the method might throw an Exception. It
also might not
What do you pass in a throw?
String parameter with message or can pass no parameters
throw new Exception();
throw new Exception(“Thing”);
What type of expection is NullPointerException
Run time exception
What exception does this produce?
~~~
String[] animals = new String[0];
System.out.println(animals[0]);
~~~
ArrayIndexOutOfBoundsException
What is error’s subclass of?
is the program required to declare or handle?
subclass of Error; No
What is checked exception sub class of?
is the program required to declare or handle?
Subclass of Exception but not subclass of RuntimeException.
Yes
What is run time exception sub class of?
is the program required to declare or handle?
Subclass of RuntimeException.
They don’t have to be handled or declared.
They can be thrown by the programmer or by the JVM
No.
3: void explore() {
4: try {
5: fall();
6: System.out.println(“never get here”);
7: } catch (RuntimeException e) {
8: getUp();
9: }
10: seeAnimals();
11: }
12: void fall() { throw new RuntimeException(); }
fall() calls new RuntimeException and goes directly to line 7
Does it compile?
try
fall();
catch (Exception e)
System.out.println(“get up”);
No because braces are missing.
Does it compile?
try {
fall();
}
No because catch is missing
catch is not
required if finally is present.
true
When is finally used?
finally is typically used to close resources such as fi les or databases
When does finally not get executed?
System.exit(0);.
When catching exceptions if you have superclass and subclass what should you catch first?
subclass
then super class next.
Why doesnt the following compile?
~~~
public void visitMonkeys() {
try {
seeAnimal();
} catch (ExhibitClosed e) { //superclass
System.out.print(“not today”);
} catch (ExhibitClosedForLunch e) {// DOES NOT COMPILE subclass
System.out.print(“try back later”);
}
}
~~~
Cause the super class took care of it.
Who throws ArithmeticException?
RuntimeException. JVM
Who throws ArrayIndexOutOfBoundsException?
JVM
Who throws ClassCastException?
ClassCastException
Who throws IllegalArgumentException?
programmer
Who throws NullPointerException?
JVM
Who throws NumberFormatException?
programmer
What is ArithmeticException
divide by zero
ArrayIndexOutOfBoundsException
int[] countsOfMoose = new int[3];
System.out.println(countsOfMoose[-1]);
There is no such thing as negative array index
ClassCastException
Java tries to protect you from impossible casts. This code doesn’t compile because Integer
is not a subclass of String:
String type = “moose”;
Integer number = (Integer) type; // DOES NOT COMPILE
IllegalArgumentException
IllegalArgumentException is a way for your program to protect itself.
Like if you dont want negative values
NullPointerException
Instance Variables and methods must be called on a non-null reference. If the reference is
null, the JVM will throw a NullPointerException.
runtime
NumberFormatException
trying to convert something non-numeric into an int:
Integer.parseInt(“abc”);
FileNotFoundException
Checked exception. Programmer
a file does not exist
IOException
problem reading or writing a file
what is FileNotFoundException subclass of?
IOException
Errors thrown by what?
JVM
ExceptionInInitializerError
Thrown by the JVM when a static initializer throws
an exception and doesn’t handle it
StackOverflowError
Thrown by the JVM when a method calls itself too many times
(this is called infi nite recursion because the method typically calls itself without end)
NoClassDefFoundError
Thrown by the JVM when a class that the code uses is available
at compile time but not runtime
when Java can’t find the class at runtime
What are checked exception?
Checked exceptions must be handled or declared.
Does this compile?
~~~
class CanNotHopException extends Exception { }
class Hopper {
public void hop() { }
}
class Bunny extends Hopper {
public void hop() throws CanNotHopException { }
}
~~~
No bc the subclass introduced a new exception
class CanNotHopException extends Exception { }
class Hopper {
public void hop() { }
}
class Bunny extends Hopper {
public void hop() throws CanNotHopException { } // DOES NOT COMPILE
}
System.out.println(e);
the exception type and message
System.out.println(e.getMessage());
just the messagve
e.printStackTrace();
The rest shows a stack trace
When are you required to use a finally block in a regular try statement (not a try-withresources)?
when there is no clause
Why are checked exception called checked exception?
Checked during the compilation.
Checked Exception is the subclass of?
java.lang.Exception or java.lang.RuntimeException?
java.lang.Exception
nmame runtime exceptions?
NullPointerException, ArrayIndexOutOfBoundsException, ClassCastExceptions.
why called runtime exception?
it isnt feasible to determine whether a method call with throw a runtime exception until it executes.
default value of static and instance variable
null