The throw Statement:
A throw statement has the form
throw expression;
where the type of the expression must be a subtype of class Throwable . The throw statement is executed as follows: The expression is evaluated to obtain an exception object v. If it is null, then a NullPointerException is thrown; otherwise the exception object v is thrown. Thus a thrownA throw statement has the form
throw expression;
exception is never null. In any case, the enclosing block statement terminates abruptly .
The try-catch-finally Statement:
A try-catch statement is used to catch (particular) exceptions thrown by the execution of a block of code. It has the following form:
try
body
catch (El x1) catchbody1
catch (E2 x2) catchbody2
...
finally finallybody
terminates normally, or exits by return or break or continue (when inside a method or constructor or switch or loop), then the catch clauses are ignored. If the body terminates abruptly by throwing exception e of class E, then the first matching Ei (if any) is located, variable xi is bound to e, and the corresponding catchbodyi is executed. The catchbodyi may terminate normally, or loop, or exit by executing return or break or continue, or throw an exception (possibly xi); if there is no finally clause, this determines how the entire try-catch statement terminates. A thrown exception e is never null , so xi is guaranteed not to be null either. If there is no matching Ei, then the entire try-catch statement terminates abruptly with exception e.
continue (when inside a method or constructor or switch or loop), regardless of whether any
exception thrown by body was caught by a catch clause, and regardless of whether the catch clause exited by executing return or break or continue or by throwing an exception. If execution of finallybody terminates normally, then the entire try-catch-finally terminates as determined by body (or catchbodyi, if one was executed and terminated abruptly or exited). If execution of finallybody terminates abruptly, then that determines how the entire try-catch-finally terminates .
The assert Statement:
The assert statement has one of the following forms:
assert boolean-expression ;
assert boolean-expression : expression ;
The boolean-expression must have type boolean. The expression must have type boolean, char,
double, float, int, long, or Object.
java -enableassertions C
One may put an assert statement after a particularly complicated piece of code, to check that it has achieved what it was supposed to
Using assert to Specify and Check the Result of an Algorithm:
static int sqrt(int x) { // Algorithm by Borgerding, Hsieh, Ulery
if (x < y =" 0," b =" 0x8000," bshft =" 15," v =" x;;">= (temp = (y<<1)+b>>= 1) > 0);
assert (long)y * y <= x && (long)(y+1)*(y+1) > x;
return y;
}
In a class that has a data representation invariant, one may assert the invariant at the end of every method in the class .
Using assert to Specify and Check Invariants:
class WordList {
private LinkedList strings = new LinkedList();
private int length = -1; // Invariant: equals word lengths plus interword spaces
public int length() { return length; }
public void addLast(String s) {
strings.addLast(s);
length += 1 + s.length();
assert length == computeLength() + strings.size() - 1;
}
public String removeFirst() {
String res = (String)strings.removeFirst();
length -= 1 + res.length();
assert length == computeLength() + strings.size() - 1;
return res;
}
private int computeLength() { ... } // For checking the invariant only
}
The assert statement was introduced in Java 2, version 1.4, and cannot be used in Java compilers prior to that. A program using the assert statement must be compiled with option -
source 1.4, as follows:
javac -source 1.4 myprog.java
assert res.length()==lineWidth || wordCount==1 || !wordIter.hasNext();
RELATED POST:
JAVA STATEMENTS PART TWO
No comments:
Post a Comment