Exceptions Flashcards

1
Q

What is parent class of all exception classes?

A

Throwable

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

What classes extend Throwable?

A

Exception and Error

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

What are checked exceptions?

A

Those exceptions that need to be declared or handled. They are expected and checked at compile time.

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

What are unchecked exceptions?

A

Exceptions that are optionally handled. They can appear at any time. They are not checked at compile time.

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

What main types of exceptions are unchecked exceptions?

A

Error, RuntimeException

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

What main types of exceptions are checked exceptions?

A

Throwable, Exception

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

Can you catch exceptions from methods that are empty?

A

Only if they are unchecked exceptions. Checked exceptions must be declared in method signature.

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

What do try catch need to have?

A

They must contain {}

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

How does “finally” work?

A

It executes before return, break, continue keywords in try or catch blocks, then it returns to block it came from.

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

When is catch block not necessary?

A

When you have finally block or you use try-with-resources

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

What is potential pitfall when using exceptions and its “throw” keyword?

A

Need to watch out for “throw” and “throws”

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

What is rule of overriding methods that throw a checked exception?

A

Overridden method can throw same exception, narrower exception or not declare exception at all. But it cannot declare different exception or broader exception.

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

Overridden method can throw narrower exception or not declare exception at all. Why?

A

Liskov Substitution Principle. Overridden method can potentially fix what throws a exception in super class, or can granulate problem into a subtype of the super method’s exception.

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

Name some RuntimeExceptions

A

NullPointerException, IllegalArgumentException, NumberFormatException, ArithmeticException, ArrayIndexOutOfBoundsException, ClassCastException

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

What exception does NumberFormatException extends?

A

IllegalArgumentException

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

What can throw ArithmeticException?

A

Dividing by 0

17
Q

What are Helpful NullPointerExceptions?

A

It is a new feature of NullPointerExceptions, where you can get detailed information about parameters that were reason of this exception. It shows number of parameter that threw it.

18
Q

How can we get name of params instead of numbers in Helpful NullPointerExceptions?

A

-g:vars command at java startup

19
Q

Name some Error exceptions

A

NoClassDefFoundError, ExceptionInitializerError, StackOverflowError

20
Q

Name some checked Exceptions

A

SQLException, ParseException, FileNotFoundException, IOException, NotSerializableException

21
Q

What exceptions extend IOException?

A

FileNotFoundException, NotSerializableException

22
Q

What is the rule about resources closure in try-with-resources section?

A

They are being closed from last to first

23
Q

What is the rule about resources that can be declared in try-with-resources section?

A

Classes must implement AutoCloseable interface

24
Q

What is the scope of try-with-resources?

A

Only in try block

25
Q

Can var be used in try-with-resources section?

A

Yes

26
Q

Can resources be declared outside try-with-resources section?

A

Yes, but they need to be final or effectively final

27
Q

Do try-with-resources need catch?

A

No, it is being implicitly created by compiler

28
Q

What do we need to watch out when using try-with-resources?

A

Not to close resources that are needed later, or already closed

29
Q

What are stacked or suppressed exceptions?

A

If multiple exceptions are occurred while throwing first exception, first exception is main exception, and others are considered suppressed but readable

30
Q

When are resources being closed when using try-with-resources?

A

Right after try block ends

31
Q

What is implicit finally block in try-with-resources?

A

It is a finally block that compiler adds right after try and catch blocks and before explicit finally block. It contains generated resource close calls. It is ran before finally block.

32
Q

What happens if run a method that throws an exception, but as a field?

A

Then classes constructor needs to me marked that it throws that exception