6 Exceptions Flashcards

1
Q

What is another name for runtime exception?

A

unchecked exceptions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the exception where finally does not run?

A

System.exit(0);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What do you need to know about exceptions for the exam?

A
The OCA exam can define basic (made up) exceptions to show you the hierarchy. You only need to do two things with this information.
1) you must be able to recognize if the exception is a checked or an unchecked exception. 
2) you need to determine if any of the exceptions are subclasses of the others.
class AnimalsOutForAWalk extends RuntimeException { }
class ExhibitClosed extends RuntimeException { }
class ExhibitClosedForLunch extends ExhibitClosed { }

In this example, there are three custom exceptions. All are unchecked exceptions because they directly or indirectly extend RuntimeException.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What types of exceptions do you need to know for the exam?

A

You need to recognize three types of exceptions for the OCA exam:
1- runtime exceptions,
2 checked exceptions,
3- and errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Name 6 Runtime Exceptions

A

ArithmeticException Thrown by the JVM when code attempts to divide by zero
ArrayIndexOutOfBoundsException Thrown by the JVM when code uses an illegal
index to access an array
ClassCastException Thrown by the JVM when an attempt is made to cast an exception
to a subclass of which it is not an instance
IllegalArgumentException Thrown by the programmer to indicate that a method has
been passed an illegal or inappropriate argument
NullPointerException Thrown by the JVM when there is a null reference where an
object is required
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an error?

A

Errors extend the Error class. They are thrown by the JVM and should not be handled or declared. Errors are rare,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Name 3 common errors

A

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 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

T or F

Methods are free to throw any runtime exceptions they want without mentioning them in the method declaration.

A
True - The reason that it’s okay to declare new runtime exceptions in a subclass method is that
the declaration is redundant. Methods are free to throw any runtime exceptions they want
without mentioning them in the method declaration.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between throws and throw?

A

Throws is in the declaration
Throw is when it is thrown

void fall() throws Exception {
throw new Exception();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What do block and clause mean? as in try block and catch clause

A

Same thing, just the block of code between the braces for try and catch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Are braces required around the code after try and catch?

A

Yes, It will not compile without braces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Does the order of try, catch and finally blocks matter?

A

yes. It must be in the order try, catch and finally. Finally must be last.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Does finally have to be last in the try, catch, finally block?

A

yes. It will not compile if there is a finally that isn’t last.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is a catch statement required if there is a try statement?

A

Catch is not required if finally is present. It just must have one or the other and can have both.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

T or F

A

A rule exists for the order of the catch blocks. Java looks at them in the order they appear. If it is impossible for one of the catch blocks to be executed, a compiler error about unreachable code occurs. This happens when a superclass is caught before a subclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why doesn’t this compile?

class CanNotHopException extends Exception { }
class Hopper {
public void hop() { }
}
class Bunny extends Hopper {
public void hop() throws CanNotHopException { } // DOES NOT COMPILE
}
A

Because hop() is an overridden method and it can’t declare a new or [—] exception.

17
Q

If the parent class throws an exception, does the child class have to throw an exception?

A
No. A subclass is allowed to declare fewer exceptions than the superclass or interface. This is
legal because callers are already handling them.
class Hopper {
public void hop() throws CanNotHopException { }
}
class Bunny extends Hopper {
public void hop() { }
}
18
Q

If a parent class declares an exception, what are the rules for the child class to declare a different exception?

A

The child class exception must be a subclass of the exception type (it must be more specific).

class Hopper {
public void hop() throws Exception { }
}
class Bunny extends Hopper {
public void hop() throws CanNotHopException { }
}
19
Q

Can a new runtime exception be declared in a subclass?

A
it’s okay to declare new runtime exceptions in a subclass method is that
the declaration is redundant. Methods are free to throw any runtime exceptions they want
without mentioning them in the method declaration.
20
Q

Write the code to have print out the exception

1) What Java prints by default
2) Just the exception message
3) Stack trace

A
} catch (Exception e) {
  System.out.println(e); //1
  System.out.println(e.getMessage()); //2
  e.printStackTrace(); //3
 }
21
Q

When exceptions are thrown in try, catch AND finally, which one is thrown in the end?

Example:

26: try {
27: throw new RuntimeException();
28: } catch (RuntimeException e) {
29: throw new RuntimeException();
30: } finally {
31: throw new Exception();
32: }

A
  • the one is try is thrown and caught

- the one is catch gets ignored and the one in finally is thrown

22
Q

Return statements in try catch block.
Every method has a return type. So in a method with try/catch there must be the correct return statement at the end. Explain how return statement must be included in a method with try/catch.

A

-If the end of the method (outside try/catch) will for sure be reached then the return statement can be there only.
-If the return statement is not at the end of the method, it needs to be in all possible paths of the try catch block.
try (successful) –> return x;
try(fail) catch(successful)–> return x;

the return statement CAN be in finally but the compiler says it is not normal.

The return statement CANNOT be in the try /catch AND the last line of the method. It would be unreachable.

It CAN be in the catch only and the last line of the method.

If there is both catch and finally, and catch has a return statement, finally will execute then it will go back to catch for the return statement.

The method will be exited wherever the return statement is. If return is hit in the try/catch, anything else in the method below try catch will not get executed.

23
Q

T or F When an exception is NOT caught but there is a finally block, the method will continue to the end.

A

False, if the exception is not caught, finally executes and the method stops

24
Q

When is ExceptionInInitializationError thrown?

A

When an exception is thrown inside a static block.

class AX{
  static int[] x = new int[0];
  static{
   x[0] = 10;
  }
  public static void main(String[] args){
    AX ax = new AX();
  }
}
25
Q

What are the only kind of Exceptions static and instance initializers can throw?

A

Runtime Exceptions

26
Q

What are 2 checked exceptions?

A

FileNotFoundException

IOException

27
Q
Is this legal?
class Animal throws RuntimeException{}
A

No! throws can only be in the method signature or constructor, not in a class declaration.

28
Q
T or F
If the super class doesn't have any checked exceptions, the subclass can't have any either.
A

True

29
Q

For exceptions, give example how to:
1-You can let Java print it out
2- print just the message
3-or print where the stack trace comes from.

A
try {
  hop();
  } catch (Exception e) {
   System.out.println(e);
 System.out.println(e.getMessage());
 e.printStackTrace();
 }

java.lang.RuntimeException: cannot hop
cannot hop
java.lang.RuntimeException: cannot hop
at trycatch.Handling.hop(Handling.java:15)
at trycatch.Handling.main(Handling.java:7)
The fi rst line shows what Java prints out by default: the exception type and message. The
second line shows just the message. The rest shows a stack trace.
The stack trace is usually the most helpful one because it shows where the exception occurred in each method that it passed through. On the OCA exam, you will mostly see the first approach. This is because the exam often shows code snippets.

30
Q

What is the result?

class A_Parent {
	public A_Parent(){
	  new B_child();
	  System.out.println("making A");
	}
}
class B_child extends A_Parent {
	public B_child(){
		super();
		System.out.println("making B");
	}

}

A

StackOverflowError - NOT compiler error