Flow Control, Exceptions and Assertions
Flow Control, Exceptions and Assertions
Flow Control, Exceptions and Assertions
Assertions
Objectives
• Write code using if and switch statements, and identify legal argument types for these
statements.
• Write code using all forms of loops, including labeled and unlabeled use
of break and continue, and state the values taken by loop counter variables during and after
loop execution.
• Write code that makes proper use of exceptions and exception handling clauses (try-catch-
finally), and declares methods and overriding methods that throw exceptions.
• 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.
Control Flow Statements
You can have zero or one else for a given if, and it must
come after any else ifs.
You can have zero to many else ifs for a given if and they
must come before the (optional) else.
Once an else if succeeds, none of the remaining else ifs or
elses will be tested
Sample
final int a = 1;
final int b:
b =2;
int x = 0;
switch (x) {
case a:
case b:
Sample
byte g = 2;
switch(g) {
case 23:
case 128:
}
---------------------------------------------------------------------------------------------------------
int temp = 90;
switch(temp) {
case 80 : System.out.println("80");
case 80 : System.out.println("80"); // won't compile!
case 90 : System.out.println("90");
default : System.out.println("default");
}
Loops and Iterators
while statement
do-while statement
for statement
while Loops
The while statement continually executes a block of statements while a particular condition is
true.
The while loop is good for scenarios where you don't know how many times a block or statement
should repeat, but you want to continue looping as long as some condition is true.
Its syntax can be expressed as:
while (expression)
{
statement(s)
}
The while statement evaluates expression, which must return a boolean value. If the
expression evaluates to true, the while statement executes the statement(s) in the while
block. The while statement continues testing the expression and executing its block until the
expression evaluates to false
do Loops or do-while Loops
The for loop is especially useful for flow control when you
already know how many times you need to execute the
statements in the loop's block. The for loop declaration has
three main parts, besides the body of the loop:
for(declaration : expression)
reutnm
Handling Exceptions
1. try {
2. // This is the first line of the "guarded region"
3. // that is governed by the try keyword.
4. // Put code here that might cause some kind of exception.
5. // We may have many code lines here or just one.
6. }
7. catch(MyFirstException) {
8. // Put code here that handles this exception.
9. // This is the next line of the exception handler.
10. // This is the last line of the exception handler.
11. }
12. catch(MySecondException) {
13. // Put code here that handles this exception
14. }
15.
16. // Some other unguarded (normal, non-risky) code begins here
finally
1: try {
2: // This is the first line of the "guarded region".
3: }
4: catch(MyFirstException) {
5: // Put code here that handles this exception
6: }
7: catch(MySecondException) {
8: // Put code here that handles this exception
9: }
10: finally {
11: // Put code here to release any resource we
12: // allocated in the try clause.
13: }
14:
15: // More code here
finally
try {
// do stuff
} finally {
//clean up
}
Exception Handling
• The parent class used depends on how the new exception will
be used
Exception Hierarchy
Handling an Entire
Class Hierarchy of Exceptions
try {
// Some code here that can throw a boundary exception
}
catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
Handling an Entire
Class Hierarchy of Exceptions
try {
// some code
}
catch (Exception e) {
e.printStackTrace();
}
Exception Declaration and the
Public Interface
try {
callBadMethod();
} catch (Exception ex) { }
Rethrowing the Same Exception
catch(IOException e) {
// Do things, then if you decide you can't handle it…
throw e;
}
----------------------------------------------------------------------------
public void doStuff() {
try {
// risky IO things
} catch(IOException ex) {
// can't handle it
throw ex; // Can't throw it unless you declare it
}
}
Where Exceptions Come From