Handling Exceptions Flashcards
True/false: A try block must be accompanied by either a catch block or a finally block or both.
True
True/false A catch must have a exception: catch(SomeException se){ }
True
True/false: There can be anything between a catch or a finally block and the closing brace of the previous try or catch block.
False
- There cannot be any thing between a catch or a finally block and the closing brace of the previous try or catch block.
True/false: You can have any number of catch blocks in any order.
True
- but each catch must be of a different type.
True/false: A catch for a subclass exception should occur before a catch block for the superclass exception.
True
True/false: Finally cannot occur before any catch block.
True
Which of these statements are true:
- If a RuntimeException is not caught, the method will terminate and normal execution of the thread will resume.
- An overriding method must declare that it throws the same exception classes as the method it overrides.
- The main method of a program can declare that it throws checked exceptions.
- A method declaring that it throws a certain exception class may throw instances of any subclass of that exception class.
- finally blocks are executed if and only if an exception gets thrown while inside the corresponding try block.
3, 4
- Normal execution will not resume if an exception is uncaught by a method. The exception will propagate up the method invocation stack until some method handles it. If no one handles it then the exception will be handled by the JVM and the JVM will terminated that thread.
Which of these represents runtimeExceptions. 1. RuntimeException 2. CheckedException 3. NullPointerException 4. ArrayIndexOutOfBoundsException 5. CompilationException 6. Throwable 7. StackOverflowException 8. MemoryOutOfBoundsException 9. IllegalArgumentException 10 NumberException
1, 3, 4, 9
True/false: If a checked exception is thrown inside a catch it must be be handles by either a try catch block or declared in the throws clause of the enclosing method.
True
- Also true for any other method that throws a checked exception
What can be the type of a catch argument::
- Any class that extends java.lang.Exception
- Any class that extends java.lang.Exception except any class that extends java.lang.RuntimeException
- Any class that is-a Throwable.
- Any Object
- Any class that extends Error
3
Which of these statements are true:
- If a RuntimeException is not caught, the method will terminate and normal execution of the thread will resume.
- An overriding method must declare that it throws the same exception classes as the method it overrides.
- The main method of a program can declare that it throws checked exceptions.
- A method declaring that it throws a certain exception class may throw instances of any subclass of that exception class.
- finally blocks are executed if and only if an exception gets thrown while inside the corresponding try block.
3, 4