|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
finally clauses and the bytecodes that are relevant to these clauses.As the Java virtual machine executes the bytecodes that represent a Java program, it may exit a block of code -- the statements
between two matching curly braces -- in one of several ways. For one, the JVM simply could execute past the closing curly
brace of the block of code. Or, it could encounter a break, continue, or return statement that causes it to jump out of the
block of code from somewhere in the middle of the block. Finally, an exception could be thrown that causes the JVM either
to jump to a matching catch clause, or, if there isn't a matching catch clause, to terminate the thread. With these potential
exit points existing within a single block of code, it is desirable to have an easy way to express that something happened
no matter how a block of code is exited. In Java, such a desire is expressed with a try-finally clause.
To use a try-finally clause:
try block the code that has multiple exit points, andfinally block the code that must happen no matter how the try block is exited.For example:
try {
// Block of code with multiple exit points
}
finally {
// Block of code that is always executed when the try block is exited,
// no matter how the try block is exited
}
If you have any catch clauses associated with the try block, you must put the finally clause after all the catch clauses, as in:
try {
// Block of code with multiple exit points
}
catch (Cold e) {
System.out.println("Caught cold!");
}
catch (APopFly e) {
System.out.println("Caught a pop fly!");
}
catch (SomeonesEye e) {
System.out.println("Caught someone's eye!");
}
finally {
// Block of code that is always executed when the try block is exited,
// no matter how the try block is exited.
System.out.println("Is that something to cheer about?");
}
If during execution of the code within a try block, an exception is thrown that is handled by a catch clause associated with the try block, the finally clause will be executed after the catch clause. For example, if a Cold exception is thrown during execution of the statements (not shown) in the try block above, the following text would be written to the standard output:
Caught cold! Is that something to cheer about?
In bytecodes, finally clauses act as miniature subroutines within a method. At each exit point inside a try block and its associated catch clauses, the miniature subroutine that corresponds to the finally clause is called. After the finally clause completes -- as long as it completes by executing past the last statement in the finally clause, not by throwing an exception or executing a return, continue, or break -- the miniature subroutine itself returns.
Execution continues just past the point where the miniature subroutine was called in the first place, so the try block can be exited in the appropriate manner.