Section 2: Flow Control, Assertions, and Exception Handling

  • Write code using if and switch statements and identify legal argument types for these statements.

  • Recognize the effect of an exception arising at a specified point in a code fragment. Note: The exception may be a runtime exception, a checked exception, or an error (the code may include try, catch, or finally clauses in any legitimate combination).

  • Write code that makes proper use of assertions, and distinguish appropriate from inappropriate uses of assertions. Identify correct statements about the assertion mechanism.

Relevant Sections

Read Section 5.2 "Selection Statements" and all sections from 5.5 "Stack-based Execution and Exception Propagation" to the end of the chapter.

Study Notes

The if statement affects control flow based on a boolean expression. The switch statement affects control flow based on a non-long integral expression. The break statement exits the loop or switch statement.

There are three basic control flow scenarios that may occur in conjunction with exceptions. The control flow for each of these scenarios should be understood:

  1. When no exception is generated.

  2. When an exception is thrown within a try block, and a catch block handles the exception.

  3. When an exception is thrown within a try block, and no catch block handles the exception.

The finally block is always executed.

The boolean expression of an assert statement will be evaluated if assertions are enabled during runtime, and an java.lang.AssertionError will be thrown if the result is false. An augmented form of the assert statement allows a value to be displayed as a detailed error message. AssertionError is a direct subclass of Error. The assertion mechanism should be used to write correct programs, and the exception facility should be used to make them robust.



     
    ASPTreeView.com
     
    Evaluation has О·НРТД¶Жexpired.
    Info...