Chapter 6: Exceptions and Assertions Flashcards

1
Q

Checked Exceptions

A

java. text.ParseException (Converting String to number)
java. io.IOException
java. io.FileNotFoundException
java. io.NotSerializableException
java. sql.SQLException

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

Runtime Exceptions

A

ArrayStoreException (Storing wrong data in an array)
DateTimeException (Invalid format string for a data)
MissingResourceException (Access a key or resource bundle which doesn’t exist)
IllegalStateException/UnsupportedOperationException: Running an invalid operation on Collections and Concurrency.

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

Keys points from OCA

A
Java looks at catch blocks in order, so you can't have a subclass after a superclass since this is unreachable code.
Can't have a catch block for a checked Exception when tone can't be thrown (unreachable code)
If a method can throw an exception, must declare it throws at least that.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Creating your own Exception

A

Checked - extend Exception

Runtime - extend Runtime Exception

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

Multi catch

A
try {} catch (Exception 1 | Exception 2 e) {}
Can be listed in an order, but can only have one variable at the end.
Multi catch is meant for unrelated Exceptions.
E.g. if one type is a subclass of the other, it won't compile.
Can't reassign the variable in a multi-catch clause. But you can with a single-catch clause.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Try-with-resources

A
Permitted to omit catch and finally clauses. E.g. try(Resource to be closed) {} 
e.g. try (BufferedReader r = Files.newBufferredReader(path1))
Resource is closed automatically by Java.
The resource is only available in the try clause.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Auto Closeable

A

Class needs to extend AutoCloseable in order for Java to be able to close automatically in a try-with-resources block.
Implementation needs to override void close() {} throws Exception method. Note: implementation can throw any more specific exception that they choose.
If method throws Exception, the method of the caller needs to throw it too.

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

Suppressed Exceptions

A

When multiple exceptions are thrown, all but the first one are called suppressed exceptions.
What if we have 2 resources we’re trying to close and they both throw Exceptions? Java tried to close them in.reverse order, then the first one throws the primary Exception while the other is suppressed.
Note: the Java catch block will only ever try to match on the primary exception.

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

Assertions

A
assert boolean_expression
assert expression : error_message; You're allows () around the boolean expression.
You can enabled/disable assertions by running java -ea program.
You can run with just a single class, or subclasses having them enabled e.g. java -ea: path.to.class Program
When running from command line if you don't specific -ea then assertions are not enabled.
java -da -ea: path.to.class disabled them globally but enables them for a single class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly