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?
What are Rules to Assertions?
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?
catch(SQLException e | IOException e){… –> won’t compile
Does order matter in a multi catch?
Can the exceptions match each other in a multi catch?
catch(FileNotFoundException | IOException e){.. --> won’t compile, since
FileNotFoundException is a subclass of IOExceptionCan you reassing the catch parameter(Exception e)?
Does the try must have a catch of final?
A try must have catch or finally, but the try with resources statement is logically calling a
finally block to close the reader
Does the try-with-resources automatically calls close?
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
What happens when more then one exception is thrown in a try-with-resources block?
If more than one exception is thrown in a try-with-resources block, it gets added as a
suppressed exception