Exceptions and Assertions Flashcards

1
Q

Throw and Throws definition

A

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

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

What should we decide on creating our own exception class?

A

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

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

Three most common constructors defined by the Exception classes

A
1. public someclass(){
{super();}
2. public someclass(Exception e){
super(e);
}
3. public someclass(String message){
super(message);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to print the stack trace of our own?

A

e.printStackTrace();

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

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

A
  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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the approach after 1.7 for catching multiple exceptions?

A

Use multi-catch approach

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

Syntax of multi-catch

A
1. try{
}
catch(Exception 1 | Exception 2 e){
// exception handler
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  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?

A

2 one compiles.

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

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

A

No, compiler throws the error

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

catch(FileNotFoundException | IOException e).

What happens?

A

Exception is thrown.

The Exception FileNotFoundException is already caught by the alternative IOException

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

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();
}
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
Is this allowed?
try{
throw new IOException();
}catch (IOException | RunTimeException e){
e=new RuntimeException();
}
A

No, It is not allowed in multicatch statements.

Multi-catch are effectively final.

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

What is the issue with finally clause?

A

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

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

what does java 7 introduces for managing the resources automatically?

A

try-with-Resources approach

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

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

A

Automatic Resource Management

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

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

A

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

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

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

A

No, it is not legal,

18
Q

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

A

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

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

what is the output?

A

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

20
Q

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

A

The resource should implement AutoCloseable interface.

21
Q

What will happen here?

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

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

22
Q

What is the method to be overridden for AutoCloseable interface?

A

public void close() throws Exception;

23
Q

what is idempotent?

A

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

24
Q

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?

A

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
Q

What does Java recommends for close method()

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

What are the difference between Closeable and AutoCloseable interface?

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

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

A

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
Q

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

A

All but the first exceptions are called as ‘Suppressed Exceptions’

29
Q

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());
			}
		}
	}
A

caught: java.lang.IllegalStateException: This is primary

Suppressed

30
Q

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

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

what is the output of thie below?

static int i = 0;
	@Override
	public void close() throws RuntimeException{
		throw new RuntimeException("from close " + i++ );
	}
	public static void main(String[] args) { //throws Exception{
		try(AutoCloseableIssue issue1 = new AutoCloseableIssue();
				AutoCloseableIssue issue2 = new AutoCloseableIssue()){
			System.out.println("Hello");;
		}
		catch(RuntimeException e){
			System.out.println("caught: " + e);
			for(Throwable e1 : e.getSuppressed()){
				System.out.println(e1.getMessage());
			}
		}
	}
A

Hello
caught: java.lang.RuntimeException: from close 0
from close 1

32
Q

What does the suppressed exception applicable only to?

A

The suppressed exception is applicable only to ‘try clause’

33
Q
What exception does this throw?
static int i = 0;
	@Override
	public void close() throws RuntimeException{
		throw new RuntimeException("from close " + i++ );
	}
	public static void main(String[] args) { //throws Exception{
		try(AutoCloseableIssue issue1 = new AutoCloseableIssue();
				AutoCloseableIssue issue2 = new AutoCloseableIssue()){
			System.out.println("Hello");;
		}
		finally{
			throw new RuntimeException("And we lost others");
		}
A

java.lang.RuntimeException: And we lost others

at com.org.AutoCloseableIssue.main(AutoCloseableIssue.java:17)

34
Q

What should we keep in mind always on using try-with-resources statement

A
  1. Resources are closed after the try clause ends and before any catch/finally clause runs
  2. Resorces are closed in reverse order from which they are created.
35
Q

What to keep on mind on rethrowing an exception?

A

If we call a method that throws some exception, we can catch them by having
catch(Exception e) in our code.

Java considers only the exceptions that are thrown.

Which means ‘Exception type’ in the catch block, but it really means limited set of exceptions and doesnt need to keep on changing the catch blocks while adding or updating any exceptions in method signature.

More info on 305-306 page

36
Q

What are assertions?

A

Assertions are boolean expressions that you place at a point that you consider to be true

37
Q

Syntax for Assert expressions?

A

assert boolean_expression

assert boolean_expression : error_message

38
Q

What type of error does assertion throws if it fails?

A

AssertionError. Since programmers cannot catch Error, assertion failures are fatal and ends the program

39
Q

command to enable assertions

A

java -ea Class_name
(or)
java -enableassertions class_name

40
Q

Which are the all the classes the assertions are enabled when using -ea without arguments

A

all classes except System class

41
Q

syntax for enabling the assertins only for classes in a package and its sub packages

A

java -ea package_name… class_name.Main

Close look on 3 dots. It means any class in package or subpackages

42
Q

How can we disable assertions?

A

-da or -disableassertions