3. Assertions and Java Exceptions Flashcards

1
Q

How do Assertions work?

A

Assertions work quite simply. You always assert that something is true. If it is, no problem. But if you
assertion turns out to be wrong, then an AssertionError is thrown.

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

Assertion Example:

A

Suppose you assume that a number passed into a method will never be negative. You can make that
sure with using an if/else statement, or you can use this:

private void methodA(int num){ assert (num >= 0); num+10;}

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

How to enable Assertions?

A

You can enable and disable assertions. At runtime assertions are disabled by default. So checkout for
questions that use assertions without enabling them:

1) Compile your class

2) You enable assertions at runtime with this command: java -ea or you can use
this command: java -enableassertions
- For disabling assertions, use this command: java -da or you can use this
command: java -disableassertions

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

Various command line switches for assertions?

A
- With no arguments (as in the preceding examples): Enables or disables assertions in all
classes, except fort he system classes.
- With a package name: Enables or disables assertions in the package specified and in any
packages below this package in the same directory hierarchy
- With a class name: Enables or disables assertions in the class specified
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to combine Assertion switches?

A
You can combine switches to, say, disable assertions in a single class but keep them enables for all
others as follow: java -ea -da:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to use Assertions Appropriately?

A
  • Don’t use assertion to validate arguments to a public method, but to validate arguments to a
    private method
  • Do not catch the assertion error
  • Don’t use assertions to validate command-line arguments
  • Do use assertions, even in public methods, to check for cases that you know are never, ever
    supposed to happen:
  • Don’t use assert expressions that can cause side effects:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are Rules to Assertions?

A
  • You can only use boolean expressions with an assert
  • You can add a second expression separated from the first by a colon – this expression’s string
    value is added to the stack trace
  • If you use expression2 (the value to print in the stack trace) you can call a method in that
    part, but your method must return something otherwise it won’t compile
  • An assert expression should leave the program in the same state it was in before the
    expression.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to handle more exceptions?

A

Handling more exceptions nice and easy with a feature called multi-catch (multiple catch):

catch(SQLException | IOException e){…

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

Is a catch block optional in a try with resources statement?

A

The try-with-resources statement is logically calling a finally block to close the reader. Therefore a
catch is optional in a try-with-resources statement. In a normal try catch statement you must define
a finally or catch block.

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

Can you declare any object in a try-with-resources statement?

A
Because java is a statically typed language, it does not let you declare just any type in a try-with-
resources statement (for example a String object). You can only define classes who implements
AutoCloseable or Closeable. You have to override the close() method, and they throw an exception
(AutoCloseable – Exception, Closeable – IOException). You have to catch these or throw it to.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the close() method of AutoClosable throw?

A

Exception

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

What does the close() method of Closable throw?

A

IOException

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

Can you use variable names multiple times in a multi catch?

A
  • You can’t use the variable name multiple times in a multi-catch:

catch(SQLException e | IOException e){… –> won’t compile

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

Does order matter in a multi catch?

A
  • With multi-catch the order doesn’t matter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can the exceptions match each other in a multi catch?

A
  • With multi-catch, you have to make sure a given exception can only match one type. Multi-
    catch is only for exceptions in different inheritance hierarchies.
catch(FileNotFoundException | IOException e){.. --> won’t compile, since
FileNotFoundException is a subclass of IOException
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can you reassing the catch parameter(Exception e)?

A
  • In a multi-catch you can’t reassign the catch parameter. Since multi-catch uses multiple
    types, there isn’t a clearly defined type for the variable that you can set. Java solves this by
    making the catch parameter final when that happens. (GELDT NIET VOOR SINGLE-CATCH)
17
Q

Does the try must have a catch of final?

A

A try must have catch or finally, but the try with resources statement is logically calling a
finally block to close the reader

18
Q

Does the try-with-resources automatically calls close?

A
Try-with-resources automatically cals close() on any resources declared in the try as
try(Resource r = new Foo()) in reversed order before going on to catch of finally
19
Q

What happens when more then one exception is thrown in a try-with-resources block?

A

If more than one exception is thrown in a try-with-resources block, it gets added as a
suppressed exception