OCA chapter 6
ExceptionInInitializerError Exception In Initializer Error
1- Java runs static initializers the first time a class is used. 2- If one of the static initializers throws an exception, Java can't start using the class. 3- It declares defeat by throwing an ExceptionInInitializerError static { int[] countsOfMoose = new int[3]; int num = countsOfMoose[-1]; } public static void main(String[] args) { } Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
StackOverflowError Stack Overflow Error
1- When Java calls methods, it puts parameters and local variables on the stack 2- After doing this a very large number of times, the stack runs out of room and overflow 3- This is called a StackOverflowError Most of the time, this error occurs when a method calls itself. public static void doNotCodeThis(int num) { doNotCodeThis(1); }
Object obj = new Integer(3); String str = (String) obj; System.out.println(str);
ClassCastExceptiomn, it tries to cast Integer to String
System.out.println(5 / 0)
It will run and throw an ArithmeticException
java.io.IOException
It's a checked exception java.io.IOException is thrown by many methods in the java.io package, but it is always thrown programmatically
26: try { 27: throw new RuntimeException(); 28: } catch (RuntimeException e) { 29: throw new RuntimeException(); 30: } finally { 31: throw new Exception(); 32: }
Line 27 throws an exception, which is caught on line 28. The catch block then throws an exception on line 29. If there were no finally block, the exception from line 29 would be thrown However, the finally block runs after the try block. Since the finally block throws an exception of its own on line 31, this one gets thrown. .The exception from the catch block gets forgotten about
NumberFormatException
NumberFormatException is a subclass of IllegalArgumentException Integer.parseInt("abc");
Runtime Exceptions ArithmeticException: Thrown by the JVM when code attempts to divide by zero ArrayIndexOutOfBoundsException: Thrown by the JVM ClassCastException: Thrown by the JVM NullPointerException: Thrown by the JVM IllegalArgumentException:Thrown by the programmer to indicate that a method has been passed an illegal or inappropriate argument NumberFormatException:Thrown by the programmer when an attempt is made to convert a string to a numeric type but the string doesn't have an appropriate format
Runtime exceptions extend RuntimeException. They don't have to be handled or declared. They can be thrown by the programmer or by the JVM.
ClassCastException
String type = "moose"; Object obj = type; Integer number = (Integer) obj;
System.exit
System.exit tells Java, "Stop. End the program right now. When System.exit is called in the try or catch block, finally does not run.
Finally
The finally block always executes, whether or not an exception occurs in the try block. If an exception is thrown, the finally block is run after the catch block. If no exception is thrown, the finally block is run after the try block completes. 25: try { // DOES NOT COMPILE 26: fall(); 27: } finally { 28: System.out.println("all better"); 29: } catch (Exception e) { 30: System.out.println("get up"); 31: }
Errors StackOverflowError : Thrown by the JVM when a method calls itself too many times (this is called infinite 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 ExceptionInInitializerError: Thrown by the JVM when a static initializer throws an exception and doesn't handle it
They are thrown by the JVM and should not be handled or declared
Checked Exceptions FileNotFoundException: Thrown programmatically when code tries to reference a file that does not exist IOException :Thrown programmatically when there's a problem reading or writing a file
They must be handled or declared. They can be thrown by the programmer or by the JVM.
class CanNotHopException extends Exception { } class Hopper { public void hop() { } } class Bunny extends Hopper { public void hop() throws CanNotHopException { } // DOES NOT COMPILE }
class Hopper { public void hop() throws CanNotHopException { } } class Bunny extends Hopper { public void hop() { } } A subclass not declaring an exception is similar to a method declaring it throws an exception that it never actually throws. This is perfectly legal. class Hopper { public void hop() throws Exception { } } class Bunny extends Hopper { public void hop() throws CanNotHopException { } } Bunny could declare that it throws Exception directly, or it could declare that it throws a more specifi c type of Exception ======================================================IMPORTANT class Hopper { public void hop() { } } class Bunny extends Hopper { public void hop() throws IllegalStateException { } } t it's okay to declare new runtime exceptions in a subclass method Methods are free to throw any runtime exceptions t
IllegalArgumentException
if (numberEggs < 0) throw new IllegalArgumentException( "# eggs must not be negative");
class AnimalsOutForAWalk extends RuntimeException { } class ExhibitClosed extends RuntimeException { } class ExhibitClosedForLunch extends ExhibitClosed { } try { seeAnimal(); } catch (ExhibitClosed e) { System.out.print("not today"); } catch (ExhibitClosedForLunch e) {// DOES NOT COMPILE System.out.print("try back later"); } }
if the more specifi c ExhibitClosedForLunch exception is thrown, the catch block for ExhibitClosed runs—which means there is no way for the second catch block to ever run. Because superClass exception cached earlier than child
ArithmeticException
int answer = 11 / 0; When this occurs, the JVM will throw an ArithmeticException: