flow control, exceptions & assertions
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 PresentationTRANSCRIPT
![Page 1: Flow control, Exceptions & Assertions](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/1.jpg)
FLOW CONTROL, EXCEPTIONS & ASSERTIONSBy Alguien Soy
![Page 2: Flow control, Exceptions & Assertions](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/8.jpg)
LABELED AND UNLABELED STATEMENTS Example of labeled statements:
![Page 9: Flow control, Exceptions & Assertions](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/12.jpg)
COMMON EXCEPTIONS & ERRORS Exceptions comes from:
JVM exceptions Programmatic exceptions
![Page 13: Flow control, Exceptions & Assertions](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/13.jpg)
SUMMARY
![Page 14: Flow control, Exceptions & Assertions](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/14.jpg)
USING ASSERTIONS Use to test or debug Could be enabled/disabled when the
application is deployed Example
![Page 15: Flow control, Exceptions & Assertions](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/15.jpg)
ASSERTION EXPRESSION RULES As following:
![Page 16: Flow control, Exceptions & Assertions](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/16.jpg)
ASSERTIONS: IDENTIFIER VS. KEYWORD
![Page 17: Flow control, Exceptions & Assertions](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/17.jpg)
ENABLING ASSERTIONS
![Page 18: Flow control, Exceptions & Assertions](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062410/56815c4a550346895dca4d64/html5/thumbnails/20.jpg)
THE END