3. Assertions and Java Exceptions Flashcards
How do Assertions work?
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.
Assertion Example:
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 to enable Assertions?
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
Various command line switches for assertions?
- 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 to combine Assertion switches?
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 to use Assertions Appropriately?
- 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:
What are Rules to Assertions?
- 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 to handle more exceptions?
Handling more exceptions nice and easy with a feature called multi-catch (multiple catch):
catch(SQLException | IOException e){…
Is a catch block optional in a try with resources statement?
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.
Can you declare any object in a try-with-resources statement?
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.
What does the close() method of AutoClosable throw?
Exception
What does the close() method of Closable throw?
IOException
Can you use variable names multiple times in a multi catch?
- You can’t use the variable name multiple times in a multi-catch:
catch(SQLException e | IOException e){… –> won’t compile
Does order matter in a multi catch?
- With multi-catch the order doesn’t matter
Can the exceptions match each other in a multi catch?
- 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