2) flow control and exception handling
Objective 1)
The Write code using if and switch statements and identify legal argument types for these statements.
· Unreachable statements produce a compile-time error.
while (false) {x = 3;}//won ' t compile
for (; false;) {x = 3;} Won ' t compile
if (false) {x = 3;}//would compile, to provide the ability to conditionally compile the code.
while (1==2) {...}//Fine
· A local variable in a blocks may re-declared in another, if the blocks are disjoint.
· If takes a Boolean arguments. Parenthesis Required. else is optional. else if structure provides multiple selective branching.
· Switch takes an argument of Byte, short, char or int. (Assignment compatible to int)
· Case value should being a constant expression that can is evaluated at compile time.
· Compiler checks each case value against the range of the switch expression ' s data type. The following code won ' t compile.
BYTE B;
Switch (b) {
Case:/No in range of byte
Default
}
· We need to place a broke statement in each case blocks to prevent the execution to fall through the other case blocks. But This isn't a part of switch statement and does enforced by the compiler.
· Default case can be placed anywhere. It ' ll be executed only if none of the case values match.
· Switch can be nested. Nested case labels are independent, don ' t clash and outer case labels.
· Empty switch construct is a valid construct. But any statement within the "switch block" should come under a case label or the default case label.
Objective 2)
Write code using all forms of loops including labeled and unlabeled by using the break and continue and the values taken B Y loop counter variables during and after loop execution.
· 3 Constructs–for while the Do
· All loops are controlled by a Boolean expression.
· In with and for, the test occurs on the top. In doing, test occurs at the bottom, so the ' body ' executed at least once.
· In AS, we can declare multiple variables in the ' The ' loop separated by commas, also we can have STA Tements in the third part separated by commas.
· In the ' a ' for statement, we can have a list of declaration statements or a list of expression, BU T not both. We cannot mix them.
· All expressions in the third section of statement to always execute, even if the the I expression makes the loop con Dition false. There is no short–circuit here.
· Break statement can is used with any kind the loop or a switch statement or just a labeled block.
· Continue statement can be used and only a loop (any kind of loop).
· Loops can have labels. The break statement abandons processing to the current loop entirely, the continue statement only abandons the currently P Rocessing time around the loop.
· Names of the labels follow the same rules as the name of the variables. (Identifiers)
· Labels can have the same name, as long as they don ' t enclose one another.
· There is no restriction against using the same identifier as a label and as the name of a package, class, interface, Metho d, field, parameter, or local variable.
· In fact, labels May is applied to any statements, but they-are only useful into the context of beak and continue in loop con Structions.
Objective 3)
Write code that makes proper the use of exceptions and exception handling clauses (try Catch finally) and declares methods and Overriding methods that throw exceptions.
· In no method or constructor (isn't class) that contains lines that might throw a checked exception, you must either handle The exception using a Try/catch construct, or declare that this method throws the exception. Don't need both, but compiler wouldn ' t compain that.
· Java.lang.RuntimeException and Java.lang.Error need is handled or declared.
· Use throw the new xxxexception () to throw a exception explicitly. If the thrown object is null, a nullpointerexception would be thrown at the handler.
· If a Method A () is declared as throwing of Exception by the throws clause, when you call a () in method B (), Method B () has To either throws exception or catch the exception by A ().
· An exception causes a jump to the "end of" try block. If the exception occurred in Alpha called from a try block, the called method is abandoned.
· If there ' s a catch block for the occurred exception or a parent class of the exception, the exception are now considered ha Ndled.
· The Try/catch clause must trap errors in the order their natural order of hierarchy.
· At least one ' catch ' blocks or one ' finally ' block must accompany a ' try ' statement. If all 3 blocks are present, the order is important. (try/catch/finally)
· Finally and catch can come only with try, they cannot appear on their own.
· Regardless of whether or not a exception occurred or whether or isn't it was handled, if there is a finally blocks, it ' ll be Executed always. (Even if there is a return statement in try block).
· The System.exit () and error conditions are the only exceptions where finally blocks are not executed.
· If there was no exception or the exception is handled, execution continues at the statement after the try/catch/finally b Locks.
· If the exception is isn't handled, the process repeats looking for next enclosing try blocks up the call hierarchy. If This search reaches the "hierarchy" (the point at which the thread being created), then the thread is Kill Ed and message stack trace are dumped to System.err.
· If an exception handler re-throws the exception (throw in a catch block), same rules apply. Either you need to have a try/catch within the catch or specify the entire method as throwing the exception of that ' s being r E-thrown in the catch block. Catch blocks at the same level would not handle the exceptions thrown in a catch block–it needs its own.
· If there ' s no code in try block this may throw exceptions specified in the catch blocks, compiler'll produce an error. (This isn't the case for Super-class Exception)
· In other words, a overriding method may not throw checked exceptions this are not thrown by the overridden method. A can return A Exception (?)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.