Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet175/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   171   172   173   174   175   176   177   178   ...   245
Bog'liq
Java 17 Recipes

public class Recipe9_6 {
public static void main(String []args) {
Recipe9_6 recipe = new Recipe9_6();
recipe.start();
}
private void start() {
try (
FileOutputStream fos = new FileOutputStream("out.log");
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos)
) {
dos.writeUTF("This is being written");
} catch (Exception e) {
System.out.println("Some bad exception happened ");
}
}
Chapter 9 exCeptions and Logging


342
 How It Works
In most cases, you want to cleanly close/dispose of resources acquired within a 
try/catch block after the block execution is complete. If a program does not
close/dispose of its resources or does so improperly, the resources could be acquired 
indefinitely, causing issues such as memory leaks. Most resources are limited (file 
handles or database connections), and as such, cause performance degradation 
(and more exceptions to be thrown). To avoid these situations, Java provides a means 
of automatically releasing resources when an exception occurs within a try/catch 
block. By declaring a try-with-resources block, the resource on which the try 
block was checked is closed if an exception is thrown within the block. Most of the 
resources built into Java work properly within a try-with-resources statement
(for a full list, see implementers of the java.lang.AutoCloseable interface). 
Also, third-party implementers can create resources that work with the try-with-
resources statements by implementing the AutoCloseable interface.
The syntax for the try-with-resources statement involves the try keyword
followed by an opening parenthesis and then followed by all the resource declarations 
that you want to have released in the event of an exception or when the block completes 
and ending with a closing parenthesis. You receive a compiler error if you try to declare a 
resource/variable that doesn’t implement the AutoCloseable interface. After the closing 
parenthesis, the syntax of the try/catch block is the same as a normal block.
The main advantage of the try-with-resources feature is that it allows a 
cleaner release of resources. Usually, when acquiring a resource, there are a lot of 
interdependencies (creating file handlers, which are wrapped in output streams, which 
are wrapped in buffered streams). Properly closing and disposing of these resources in 
exceptional conditions requires checking the status of each dependent resource and 
carefully disposing of it, and doing so requires that you write a lot of code. By contrast, 
the try-with-resources construct allows the JVM to properly dispose of resources, even 
in exceptional conditions.
Note a 
try-with-resources block always closes the defined resources, even 
if no exceptions were thrown.
Chapter 9 exCeptions and Logging


343

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   171   172   173   174   175   176   177   178   ...   245




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