Control Flow  «Prev  Next»


Lesson 1

Flow Control and Exception Handling in Java

Statements are the brick and mortar from which programs are built. Because of their importance, Oracle has dedicated a entire exam topic to them: "Flow Control and Exception Handling." The good news is that Java's programming statements are easy to master, and with a little study, you should do well on this part of the exam. Another major exam topic covered in this module is garbage collection, also well-defined and easy to master.

Module objectives

This module will help you to satisfy the following exam objectives:
Flow control and exception handling
  1. Write code using if and switch statements and identify legal argument types for these statements.
  2. 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.
  3. Write code that makes proper use of exceptions and exception handling clauses (try catch finally) and declares methods and overriding methods that throw exceptions.

Garbage Collection

State the behavior that is guaranteed by the garbage collection system and write code that explicitly makes objects eligible for collection.
We all make multiple decisions on a daily basis, and we often have to choose from a number of available options to make each decision. These decisions range from the complex, such as selecting what subjects to study in school or which profession to choose, to the simple, such as should I use a neural network. The option you choose that leads to a decision can potentially change the course of your life, in a small or big way. For example, if you choose to study medicine at a university, you have the option to become a research scientist; if you choose fine arts, you have the option to become a painter. You may also repeat particular sets of actions. These actions can range from studying mathematics every day to using a messaging app to connect with a friend, or passing exams at school or university in order to achieve a desired degree. In Java, the selection statements (if and switch) and looping statements (for, enhanced for, while, and do-while) are used to define and choose among different courses of action, as well as to repeat lines of code. You use these types of statements to define the flow of control in code.

Categories of exceptions: checked exceptions, runtime exceptions, and errors
Figure 6-1 : Categories of exceptions: checked exceptions, runtime exceptions, and errors

Three Main Categories

Exceptions can be divided into three main categories:
  1. Checked exceptions
  2. Runtime exceptions (unchecked exceptions)
  3. Errors
Of these three types, checked exceptions require most of your attention when it comes to coding and using methods. Normally, you should not try to catch runtime exceptions, and there are few options you can use for the errors, because they are thrown by the JVM.
It is important to have a crystal-clear understanding of these three categories of exceptions, including their similarities and differences.