Download
flow control exceptions assertions n.
Skip this Video
Loading SlideShow in 5 Seconds..
Flow control, Exceptions & Assertions PowerPoint Presentation
Download Presentation
Flow control, Exceptions & Assertions

Flow control, Exceptions & Assertions

132 Views Download Presentation
Download Presentation

Flow control, Exceptions & Assertions

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Flow control, Exceptions & Assertions By Alguien Soy

  2. 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

  3. 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

  4. 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.

  5. 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

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

  7. 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.

  8. Labeled and unlabeled statements • Example of labeled statements:

  9. 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

  10. 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”

  11. Exception declaration & Public interface • With “checked” exception, we need to declare • With “unchecked” exception, such as NullPointerException, we do not need to declare

  12. Common exceptions & errors • Exceptions comes from: • JVM exceptions • Programmatic exceptions

  13. Summary

  14. Using assertions • Use to test or debug • Could be enabled/disabled when the application is deployed • Example

  15. Assertion expression rules • As following:

  16. Assertions: identifier Vs. Keyword

  17. Enabling assertions

  18. 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

  19. 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

  20. The end