Exceptions and Assertions Flashcards Preview

Java8 > Exceptions and Assertions > Flashcards

Flashcards in Exceptions and Assertions Deck (42)
Loading flashcards...
1

Throw and Throws definition

Throw means an exception is actually being thrown
Throws indicates that the method merely has the potential to thrw that exception to the calling method

2

What should we decide on creating our own exception class?

We should decide on whether it should be checked or unchecked exceptions

3

Three most common constructors defined by the Exception classes

1. public someclass(){
{super();}
2. public someclass(Exception e){
super(e);
}
3. public someclass(String message){
super(message);
}

4

How to print the stack trace of our own?

e.printStackTrace();

5

what are the two approaches before java 1.7 for catching multiple exceptions?

1. To have multiple catch clause to catch each of the expected exceptions and handle them
2. To have a same method defined in each catch class that handles the exception

6

What is the approach after 1.7 for catching multiple exceptions?

Use multi-catch approach

7

Syntax of multi-catch

1. try{
}
catch(Exception 1 | Exception 2 e){
// exception handler
}

8

1. Catch(Exception1 e1 | Exception2 e2| Exception3 e3)
2. catch(Exception1 | Exception2 | Exception3 e)
3. catch(Exception1 e| Exception2 e| Exception3 e)

Which one compiles?

2 one compiles.

9

Does java allows you to specify redundant types in multi-catch?

No, compiler throws the error

10

catch(FileNotFoundException | IOException e).

What happens?

Exception is thrown.
The Exception FileNotFoundException is already caught by the alternative IOException

11

Can we reassign a variable in a catch block in normal single catch Block. Is the below one legal?

Eg.., try{
}
catch(RuntimeException e)
{
e = new RuntimeException();
}

Yes it is legal, this is allowed but we should avoid using like this.

12

Is this allowed?
try{
throw new IOException();
}catch (IOException | RunTimeException e){
e=new RuntimeException();
}

No, It is not allowed in multicatch statements.

Multi-catch are effectively final.

13

What is the issue with finally clause?

The finally clause may have some redundant codes because it should close all the resources

14

what does java 7 introduces for managing the resources automatically?

try-with-Resources approach

15

What is the Try-With-Resources class called as?

Automatic Resource Management

16

Can we have the catch/Finally clauses still used in try-with-resources approach?

Yes, we can still have the catc/finally with Try-with-resources approach

17

Can we have 2 or more finally blocks in traditional tryblocks?

No, it is not legal,

18

What is the scope of resources declared in try-with-resources block?

Its scope is only to try blocks. It cannot be accessed in the catch or finally blocks

19

try(Scanner s = new Scanner(System.in)){
s.nextLine();
}
catch(Exception e)
{
s.nextInt();
}
finally{
s.nextInt();
}

what is the output?

It doesnt compile. Bacuse the scope of the scanner is with in try only

20

What is the criteria of declaring a resource in try with resources block?

The resource should implement AutoCloseable interface.

21

What will happen here?

try(SomeClass class = new SomeClass())
{
System.out.println(class);
}

Exception: The resource type Turkey doesnot implement java.lang.AutoCloseable interface

22

What is the method to be overridden for AutoCloseable interface?

public void close() throws Exception;

23

what is idempotent?

Idempotent means that the method can be called multiple times without any side effects or change of state

24

public class AutoCloseableIssue implements AutoCloseable {

@Override
public void close() throws Exception{
throw new Exception();
}

public static void main(String[] args) {
try(AutoCloseableIssue issue = new AutoCloseableIssue()){
System.out.println("Hello");
}
}
}

What happens to this code?

The code doesnt compile because the close method throws an Exception and the place where this class is declared in try with resources has not caught tis Exception. So, it throws error

25

What does Java recommends for close method()

1. close() method not throw Exception
2. It is better to throw any specific Exception
3. The close method should be idempotent

26

What are the difference between Closeable and AutoCloseable interface?

1. The closeable restricts the specific type of exception thrown to be IOException
2. Closeable requires implementation to be Idempotent

27

What should we do when the close() method throws an Exception?

We must handle that exception. SO, we must have a catch block to catch that Exception.

Since, the try-with-resource automatically calls the Close(), if it throws an exception, we need to handle it with catch clause.

28

When multiple exceptions are thrown in try-with-resources, what are the first exception called?

All but the first exceptions are called as 'Suppressed Exceptions'

29

What is the output of the below code:

@Override
public void close() throws Exception{
throw new Exception("Suppressed");
}

public static void main(String[] args) { //throws Exception{
try(AutoCloseableIssue issue = new AutoCloseableIssue()){
throw new IllegalStateException("This is primary");
}
catch(Exception e){
System.out.println("caught: " + e);
for(Throwable e1 : e.getSuppressed()){
System.out.println(e1.getMessage());
}
}
}

caught: java.lang.IllegalStateException: This is primary
Suppressed

30

What happens if the primary exceptions thrown doesnt match with the Exception type declared in 'catch' block? Does we get compiler error?

1. It eventually throws the exception to the caller and prints the stack trace. No we don't get compiler error