Java 17 Recipes


-2. Guaranteeing a Block of Code Is Executed


Download 3.2 Mb.
Pdf ko'rish
bet170/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   166   167   168   169   170   171   172   173   ...   245
Bog'liq
Java 17 Recipes

9-2. Guaranteeing a Block of Code Is Executed
 Problem
You want to write code that executes when control leaves a code segment, even if control 
leaves due to an error being thrown or the segment ending abnormally. For example, 
you have acquired a lock and want to be sure that you are releasing it correctly. You want 
to release the lock in the event of an error or no error.
Chapter 9 exCeptions and Logging


333
 Solution
Use a try/catch/finally block to properly release locks and other resources that you 
acquire in a code segment. Place the code that you want to have executed regardless 
of exceptions into the finally clause. In the example, the finally keyword specifies a 
code block that always executes, regardless of whether an exception was thrown in the 
try block. Within the finally block, the lock is released by calling lock.unlock().
public class Recipe9_2 {
Lock myLock = new ReentrantLock();
Random random = new Random();
public static void main(String[] args) {
Recipe9_2 recipe = new Recipe9_2();
recipe.start();
}
private void start() {
for (int i = 0; i < 10; i++) {
callFunctionThatHoldsLock();
}
}
private void callFunctionThatHoldsLock() {
myLock.lock();
try {
int number = random.nextInt(5);
int result = 100 / number;
System.out.println("A result is " + result);
FileOutputStream file = new FileOutputStream("file.out");
file.write(result);
file.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
Chapter 9 exCeptions and Logging


334
} finally {
myLock.unlock();
}
}
}
 How It Works
Code placed within the finally clause of a try/catch/finally block is always executed. 
In this example, acquiring the lock at the beginning of the function and releasing it in 
the finally block guarantees that it is released at the end of the function, regardless 
of whether an exception (checked or unchecked) is thrown. In all, acquired locks 
should always be released in a finally block. In the example, suppose that the mylock.
unlock() function call was not in the finally block (but at the end of the try block); if 
an exception were to happen in this case, the call to mylock.unlock() would not happen 
because code execution would be interrupted in the location where the exception 
happened. In that case, the lock would be forever acquired and never released.
Caution if you need to return a value on a method, avoid returning values in 
the finally block. a return statement in the finally block always executes, 
regardless of any other return statements that might have happened within the 
try block.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   166   167   168   169   170   171   172   173   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling