200 likes | 370 Views
Flow control, Exceptions & Assertions. By 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:
E N D
Flow control, Exceptions & Assertions By 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 block case constants2: code block default: code block } • Notes: • A switch’s expression must evaluate to a char, byte, short, intand 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
Using assertions • Use to test or debug • Could be enabled/disabled when the application is deployed • Example
Assertion expression rules • As following:
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