FLOW CONTROL, EXCEPTIONS & ASSERTIONSBy Alguien Soy
OBJECTIVES Use if and switch statements. Use for, do and while loop statements. Use try, catch and finally statements Use assertions State the effects of exceptions Understand some common exceptions
IF STATEMENTS If statements:
if (booleanExp) {} elseif (booleanExp2) {} else { }
Notes: please be noticed that following example:***boolean boo = false;if (boo = true) { } // this is an assignment, but it compiles OK because boo is a boolean***int x = 2;if (x = 3) { } // won’t compile because x is not a boolean
SWITCH STATEMENTS Legal expressions:
switch (expression) {case constants1: code blockcase constants2: code blockdefault: code block
} Notes:
• A switch’s expression must evaluate to a char, byte, short, int and enum.
• A case constant must be a compile time constant!
• Have only one case label using the same value.
SWITCH STATEMENTS (2) Use break statement: to jump to the end of
the switch statements The default case: works just like any other
case for fall-through
LOOPS AND ITERATORS while loop:
while (expression) { //code block } do loop:
do { //code block } while (expression) for loop (as of Java 6, the for loop has 2
variables):for (/*Initialization*/ ; /*Condition*/ ; /* Iteration */) { /* loop body */}
THE ENHANCED FOR LOOP (FOR ARRAYS) for (declaration : expression) The two pieces of the for statement are
■ declaration: The newly declared block variable, of a type compatible with elements of the array you are accessing. This variable will be available within the for block, and its value will be the same as the current array element.■ expression: This must evaluate to the array you want to loop through. This could be an array variable or a method call that returns an array. The array can be any type: primitives, objects, even arrays of arrays.
LABELED AND UNLABELED STATEMENTS Example of labeled statements:
HANDLING EXCEPTIONS Using try, catch and finally statements:
try { // code block} catch (SomeException e) { // code block} finally () { // code block}
* Notes: a try statement requires at least a catch or a finally statement
EXCEPTIONS HIERARCHY
Notes: we understand all non-RuntimeException are considered “checked” exceptions, because the compiler checks to be certain you’ve acknowledged that “bad things could happen here”
EXCEPTION DECLARATION & PUBLIC INTERFACE With “checked” exception, we need to
declare
With “unchecked” exception, such as NullPointerException, we do not need to declare
COMMON EXCEPTIONS & ERRORS Exceptions comes from:
JVM exceptions Programmatic exceptions
SUMMARY
USING ASSERTIONS Use to test or debug Could be enabled/disabled when the
application is deployed Example
ASSERTION EXPRESSION RULES As following:
ASSERTIONS: IDENTIFIER VS. KEYWORD
ENABLING ASSERTIONS
USING ASSERTIONS APPROPRIATELY Do NOT use assertions to:
Validate args to a public method Validate command-line arguments Cause side effects
Do use assertions to: Validate arguments to a private method Check the case that never happen
SUMMARY An overriding method cannot throw a broader
exception than the method it’s overriding When an assert statement has two
expressions, the second expression must return a value
THE END