Java Reference
In-Depth Information
The break and continue statements. There are two statements that will
change the course of execution of the while , do-while , and for loops from
within the loop. A continue will cause execution to skip the rest of the body
of the loop and go on to the next iteration. With a for loop, this means execut-
ing the iteration expression, and then executing the test-for-termination expres-
sion. With the while and do-while loops, this means just going to the test
expression.
You can quit out of the loop entirely with the break statement. Execution
continues on the next statement after the loop.
3.2.3.3
There is one more statement that we need to cover. The return statement is
optionally followed by an expression. Execution of the current method ends at
once upon executing return , and the expression is used as the return value of
the method. Obviously, the type of the expression must match the return type
of the method. If the method is void, there should be no return expression.
The return statement
3.2.4
Errors in Java are handled through exceptions . In some circumstances, the Java
runtime will throw an exception, for example, when you reference a null
pointer. Methods you write may also throw exceptions. This is quite similar to
C++. But Java exceptions are classes. They descend from Object , and you can
write your own classes that extend an existing exception. By so doing, you can
carry up to the handler any information you would like. But we're getting ahead
of ourselves here. Let's first describe the basics of exceptions, how to catch them,
how to pass them along, and so forth.
In other programming languages a lot of code can be spent checking return
codes of function or subroutine calls. If A calls B and B calls C and C calls D,
then at each step the return value of the called function should be checked
to see if the call succeeded. If not, something should be done about the
error—though that “something” is usually just returning the error code to the
next level up. So function C checks D's return value, and if in error, returns an
error code for B to check. B in turn looks for an error returned from C and re-
turns an error code to A. In a sense, the error checking in B and C is superflu-
ous. Its only purpose is to pass the error from its origin in D to the function
that has some logic to deal with the error—in our example that's A.
Error Handling, Java Style
Search WWH ::




Custom Search