Flow control, Exceptions & Assertions

20
FLOW CONTROL, EXCEPTIONS & ASSERTIONS By Alguien Soy

description

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: - PowerPoint PPT Presentation

Transcript of Flow control, Exceptions & Assertions

Page 1: Flow control, Exceptions & Assertions

FLOW CONTROL, EXCEPTIONS & ASSERTIONSBy Alguien Soy

Page 2: Flow control, Exceptions & Assertions

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

Page 3: Flow control, Exceptions & Assertions

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

Page 4: Flow control, Exceptions & Assertions

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.

Page 5: Flow control, Exceptions & Assertions

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

Page 6: Flow control, Exceptions & Assertions

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 */}

Page 7: Flow control, Exceptions & Assertions

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.

Page 8: Flow control, Exceptions & Assertions

LABELED AND UNLABELED STATEMENTS Example of labeled statements:

Page 9: Flow control, Exceptions & Assertions

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

Page 10: Flow control, Exceptions & Assertions

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”

Page 11: Flow control, Exceptions & Assertions

EXCEPTION DECLARATION & PUBLIC INTERFACE With “checked” exception, we need to

declare

With “unchecked” exception, such as NullPointerException, we do not need to declare

Page 12: Flow control, Exceptions & Assertions

COMMON EXCEPTIONS & ERRORS Exceptions comes from:

JVM exceptions Programmatic exceptions

Page 13: Flow control, Exceptions & Assertions

SUMMARY

Page 14: Flow control, Exceptions & Assertions

USING ASSERTIONS Use to test or debug Could be enabled/disabled when the

application is deployed Example

Page 15: Flow control, Exceptions & Assertions

ASSERTION EXPRESSION RULES As following:

Page 16: Flow control, Exceptions & Assertions

ASSERTIONS: IDENTIFIER VS. KEYWORD

Page 17: Flow control, Exceptions & Assertions

ENABLING ASSERTIONS

Page 18: Flow control, Exceptions & 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

Page 19: Flow control, Exceptions & Assertions

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

Page 20: Flow control, Exceptions & Assertions

THE END