SCJP Study Notes


Flow control, Assertions and Exception Handling

  • In switch statement every case expression must be unique.
  • The argument for a "case" statement must be a constant or a constant expression which can be evaluated at compile time.
  • A variable in for loop is not visible outside of the loop.
  • The variable defined in try block can not be reached from the catch block.
  • The catch statement takes only the objects of Throwable type as an argument.
  • The finally clause of a try-catch block will always execute even if there are any return statements in the try-catch part.
  • RuntimeException and its subclasses are unchecked exceptions. They do not have to be caught.
  • A method can throw any unchecked exception, even if it is not declared in its throws clause.
  • The try-catch clause must trap errors in the order their natural order of hierarchy.
  • To treat assert as a keyword, you have to compile: javac -source 1.4 Sample.java
  • By default assert statements are disabled during normal program run.
  • To enable assertions at runtime: java -ea Sample (or java -enableassertions Sample).
  • It is not appropriate to use assertions to check the parameters of public methods.
  • As a rule, the boolean expression of an assert statement should not be used to perform actions that are required for normal operation of the program.
  • If the default label of a switch statement should not be reached under normal operating circumstances, then the default label might be a good location for an assert statement.
  • Assertions can be selectively enabled for any named class.
  • Assertions can be selectively enabled for any named package.
  • If assertions are selectively enabled for any named package then assertions are automatically enabled for the subpackages.
  • Assertions can be selectively enable for the unnamed package in the current working directory.
  • The compiler will generate an error if an assert statement is "unreachable" as defined by the Java Language Specification.
  • A catch clause should not be used to catch an AssertionError.

contents | previous | next