10.Handling Exceptions Flashcards
Is an exception thrown outside of a try block catched by other catch blocks?
For examples, multiple catch blocks, and an exception is thrown from one of the blocks…
An exception that is not thrown inside the try block, it will not be caught by any of the remaining catch blocks.
It will actually be sent to the caller of the main() method after the finally block is executed. And a stacktrace of that exception is printed.
If a RuntimeException is not caught, the method will terminate and normal execution of the thread will resume.
True
False
Any remaining code of the method will not be executed. Further, any uncaught exception will cause the JVM to kill the thread.
java.lang.SecurityException
Exists? and which is it?
true
False
Java has a java.lang.SecurityException. This exception extends RuntimeException and is thrown by the security manager upon security violation. For example, when a java program runs in a sandbox (such as an applet) and it tries to use prohibited APIs such as File I/O, the security manager throws this exception. Since this exception is explicitly thrown using the new keyword by a security manager class, it can be considered to be thrown by the application programmer.
What will be the result of attempting to compile and run the following program?
class TestClass{ public static void main(String args[]){ int i = 0; loop : // 1 { System.out.println("Loop Lable line"); try{ for ( ; true ; i++ ){ if( i >5) break loop; // 2 } } catch(Exception e){ System.out.println("Exception in loop."); } finally{ System.out.println("In Finally"); // 3 } } } }
No compilation error and line 3 will be executed.
Even if the break takes the control out of the block, the finally clause will be executed.
You can apply a label to any code block or a block level statement (such as a for statement) but not to declarations.
For example: loopX : int i = 10;
A break without a label breaks the current loop (i.e. no iterations any more) and a break with a label tries to pass the control to the given label.
‘Tries to’ means that if the break is in a try block and the try block has a finally clause associated with it then it will be executed.
What will be the result of compiling and running the following program ?
class NewException extends Exception {} class AnotherException extends Exception {} public class ExceptionTest{ public static void main(String [] args) throws Exception{ try{ m2(); } finally{ m3(); } } public static void m2() throws NewException{ throw new NewException(); } public static void m3() throws AnotherException{ throw new AnotherException(); } }
m2() throws NewException, which is not caught anywhere. But before exiting out of the main method, finally must be executed.
Since finally throw AnotherException (due to a call to m3() ), the NewException thrown in the try block ( due to call to m2() ) is ignored and AnotherException is thrown from the main method.
Consider the following code snippet: void m1() throws Exception{ try{ // line1 } catch (IOException e){ throw new SQLException(); } catch(SQLException e){ throw new InstantiationException(); } finally{ throw new CloneNotSupportedException(); // this is not a RuntimeException. } } Which of the following statements are true?
If IOException gets thrown at line1, then the whole method will end up throwing SQLException.
If IOException gets thrown at line1, then the whole method will end up throwing CloneNotSupportedException.
If IOException gets thrown at line1, then the whole method will end up throwing InstantiationException.
If no exception is thrown at line1, then the whole method will end up throwing CloneNotSupportedException.
If SQLException gets thrown at line1, then the whole method will end up throwing InstantiationException.
If IOException gets thrown at line1, then the whole method will end up throwing CloneNotSupportedException.
If no exception is thrown at line1, then the whole method will end up throwing CloneNotSupportedException.
The fundamental concepts at play here are:
- The Exception that is thrown the last, is the Exception that is thrown by the method to the caller.
So, when no exception or any exception is thrown at line 1, the control goes to finally or some catch block. Now, even if the catch blocks throws some exception, the control goes to finally.
The finally block throws CloneNotSupportedException, so the method ends up throwing CloneNotSupportedException. Other exceptions thrown by the code prior to this point are lost.
- Exception thrown by a catch cannot be caught by the following catch blocks at the same level.
So, if IOException is thrown at line 1, the control goes to first catch which throws SQLException.
Now, although there is a catch for SQLException, it won’t catch the exception because it is at the same level.
So, the control goes to the finally and same process as explained above continues.
Any exceptions thrown before this exception are lost.