|
Flow Control and Exceptions
|
Flow
control statements
- Conditional flow control statements like
if, if-else and
switch-case
- Looping statements such as for, while, do-while
- Exception handling statements like
try-catch-finally,
throw
- Ad hoc flow control statements like
break, continue
with or without label:
[More in ebook]
|
|
switch
statement
[More in ebook]
|
|
|
break and
continue
- A break statement transfers the control out
of an enclosing statement. break used within a loop
breaks the execution of the current loop. In
case of nested loops, the break statement
passes the control to the immediate outer loop.
- A continue statement breaks the current
iteration and moves to next iteration.
- break and continue with labels.
- Labels specify the target (statement)
for continue and
break.
-
continue with label
does not jump to the labeled statement
but instead jumps to the end of the
labeled loop.
- Same label identifiers can be reused
multiple times as long as they are not
nested.
- Label names do not conflict with the
same named identifier(variable, method or
class name).
[More in ebook]
|
|
Exceptions
[More in ebook]
Errors and
Exceptions
- There are two types of exceptions, an Error
and an Exception.
- An Error and an Exception are the direct
subclasses of the Throwable class.
- Errors are handled by java system.
Instances of errors are results of internal
errors inside the java runtime. Errors are rare
and usually fatal.
- All programmer-declared exceptions must be
the subclasses of Exception class.

Checked
and Unchecked Exceptions
- Checked Exceptions
- A checked exception is direct or
indirect subclass of Exception excluding
class RuntimeException and its subclasses.
- Checked Exceptions are checked by the
compiler to see if these exceptions are
properly caught or specified. If not, the
code will fail to compile.
- Checked exception forces client program
to deal with the scenario in which an
exception may be thrown.
- Checked exceptions must be either
declared or caught at compile time.
- Unchecked Exceptions
[More in ebook]
Handling
an exception
For handling exception programmatically, enclose
the statements that might throw an exception
within a try block.
try {
.
.
}catch(Throwable exception) {
}
try {
.
.
}finally {
}
-
The
try {..} block.
- A try statement executes a block and
oversees the execution of enclosed
statements for exceptions. It also defines
the scope for the exception handlers
(defined in catch clause).
- The try block must be accompanied by at
least one catch block or one finally block.
-
The
catch(Throwable throwable){..} clause of try
block.
- The catch clause contains the exception
handling code.
- The catch statement takes only the
objects of Throwable type as an argument.
- Statements in a catch block are
executed whenever a particular exception it
is catching actually occurs within the try
block.
-
Order of the exception
handlers.
The special exception handlers should
come before the generalized exception
handler. Otherwise a compile time
exception occurs.
-
The
finally {..} clause.
- A try block can have a finally clause
which must come after all the catch
clauses.
- The finally clause always gets
executed...
- In case the try block completed
normally i.e. no exception occurred
within try block.
- If exception occurs within try
block and one of the catch blocks
handled that exception.
- In case, an exception occurred
within try block that none of the catch
block could handle.
Thus, the code in finally clause is
always executed, unless the thread
executing the try code dies.
- The finally clause also helps in
general cleanup and disposal of system
resources at the time of try block exit.
-
throws clause in method declaration.
It is sometimes desirable for method to
declare which exceptions it is going to throw
instead of handling them. The client of the
method can then decide how to handle the
exception. The throws keyword is used
to identify the list of possible exceptions
that a method might throw.
|
|