Professional Documents
Culture Documents
Scjp-Chapter 4: Monday, April 19, 2021 Instructor: Khanhdm
Scjp-Chapter 4: Monday, April 19, 2021 Instructor: Khanhdm
Version: 1.0
Author: KhanhDM
Instructor: KhanhDM
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 1 04e-BM/NS/HDCV/FSOFT v2/2
Introduction
Duration: 2 hours
Purpose: Flow Control, Exceptions, and Assertions
Audience: To be Developer
Test: Self test
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 2 04e-BM/NS/HDCV/FSOFT v2/2
Objectives
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 3 04e-BM/NS/HDCV/FSOFT v2/2
Contents
Course Agenda
Two-Minute Drill
Self Test
Practice
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 4 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill
❑ if-else Branching
❑ switch Statements
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 5 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill
if-else Branching
❑ Example:
if (x > 3) {
System.out.println("x is greater than 3");
} else {
System.out.println("x is not greater than 3");
}
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 6 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill
switch Statements
❑ Another way to simulate the use of multiple if statements is with the switch statement.
❑ Example:
int x = 3;
If (x == 1) {
System.out.println("x equals 1");
} else if (x == 2) {
System.out.println("x equals 2");
} else if (x == 3) {
System.out.println("x equals 3");
} else {
System.out.println("No idea what x is");
}
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 7 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill
switch Statements
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 8 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill
switch Statements
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 9 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill
switch Statements
❑ break keyword
❑ default case
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 10 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 11 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill
❑ The declaration and initialization happens before anything else in a for loop.
❑ The scope of variables declared in the for loop ends with the for loop!
❑ Can have only one test expression.
for (int x = 0; (x > 5), (y < 2); x++) { } // too many
//expressions
❑ Using break and continue in for Loops
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 12 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill
C. Handling Exceptions
❑ Syntax:
try {
// This is the first line of the "guarded region"
// that is governed by the try keyword.
// Put code here that might cause some kind of exception.
// We may have many code lines here or just one.
} catch(MyFirstException) {
// Put code here that handles this Exception.
// This is the next line of the exception handler.
// This is the last line of the exception handler.
} catch(MySecondException) {
// Put code here that handles this exception
}
// Some other unguarded (normal, non-risky) code begins here
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 13 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill
❑ A finally block encloses code that is always executed at some point after the
try block, whether an exception was thrown or not.
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 14 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill
❑ call stack
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 15 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill
Exception Hierarchy
❑ The handlers for the most specific exceptions must always be placed above
those for more general exceptions.
try {
// do risky IO things
} catch (IOException e) {
// handle general IOExceptions
} catch (FileNotFoundException ex) {
// handle just FileNotFoundException
}
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 16 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill
❑ The throws keyword is used as follows to list the exceptions that a method can throw
void myFunction() throws MyException1, MyException2 {
// code for the method here
}
❑ Any method that might throw an exception (unless it’s a subclass of RuntimeException)
must declare the exception.
void doStuff() {
doMore();
}
void doMore() {
throw new IOException();
}
❑ Runtime exceptions are referred to as unchecked exceptions. All other exceptions,
meaning all those that do not derive from java.lang.RuntimeException, are checked
exceptions.
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 17 04e-BM/NS/HDCV/FSOFT v2/2
Questions and Answers
© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 18 04e-BM/NS/HDCV/FSOFT v2/2