[Java] - Exception & Loging
December 16, 2017 |
Before Java SE 7 or 8, we don't have any way manage a resource object (BufferReader, FileReader,... ). We must manage them by manually. Sometime, we forget close a resource and make memory leak, low performance. In Java SE 7, 8, 9, we have a way manage them by automatically.
Overview
Within JVM has two types: Check and Uncheck.
- Check exception at compile time.
- Unchecked exception at runtime.
Error exception cannot catch like: OutOfMemoryError,...
Statement:
try {//body
} catch (Exception ex) {
// exception code
} finally {
// final code
}
Handle UncatchException
======
private void start() {
Thread.setDefaultUncaughtExceptionHandler((Thread t, Throwable e) -> {
System.out.println("Woa! there was an exception thrown somewhere! " + t.getName() + ": " + e);
});
final Random random = new Random();
for (int j = 0; j < 10; j++) {
int divisor = random.nextInt(4);
System.out.println("200 / " + divisor + " Is " + (200 / divisor));
}
}
Thread.setDefaultUncaughtExceptionHandler((Thread t, Throwable e) -> {
System.out.println("Woa! there was an exception thrown somewhere! " + t.getName() + ": " + e);
});
final Random random = new Random();
for (int j = 0; j < 10; j++) {
int divisor = random.nextInt(4);
System.out.println("200 / " + divisor + " Is " + (200 / divisor));
}
}
======
private void startForCurrentThread() {
Thread.currentThread().setUncaughtExceptionHandler((Thread t, Throwable e) -> {
System.out.println("In this thread " + t.getName() + " an exception was thrown " + e);
});
Thread someThread = new Thread(() -> {
System.out.println(200 / 0);
});
someThread.setName("Some Unlucky Thread");
someThread.start();
System.out.println("In the main thread " + (200 / 0));
}
Thread.currentThread().setUncaughtExceptionHandler((Thread t, Throwable e) -> {
System.out.println("In this thread " + t.getName() + " an exception was thrown " + e);
});
Thread someThread = new Thread(() -> {
System.out.println(200 / 0);
});
someThread.setName("Some Unlucky Thread");
someThread.start();
System.out.println("In the main thread " + (200 / 0));
}
I. try-with-resource statement
try-with-resource statementtry (Resource resource 1;
Resource resourcce 2) {
}
In Java SE 6 with Resource Management.
// Java SE 6 or later
BufferedReader bufferReader1 = null;
try {
bufferReader1 = new BufferedReader(new FileReader("Test.txt"));
System.out.println(bufferReader1.readLine());
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
bufferReader1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
In Java SE 7 or later with Resource Management.
// Java SE 7 & 8
try (BufferedReader bufferReader2 = new BufferedReader(new FileReader("Test.txt"))) {
System.out.println(bufferReader2.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
II. Create a new Resource
When you create a resource class that use to try-with-resource, those class must be implement close method from java.lang.AutoCloseable .
Example:
ReadFile.java
package javaone.core.learning;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile implements AutoCloseable {
private BufferedReader buff;
private FileReader fileReader;
public void printContentOfFile(String path) {
try {
fileReader = new FileReader(path);
buff = new BufferedReader(fileReader);
String line = buff.readLine();
System.out.println(line);
while (line != null) {
System.out.println(line);
line = buff.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/** {@inheritDoc} */
@Override
public void close() throws Exception {
buff.close();
System.out.println("Close Read File");
}
}
MainTest.java
public static void main(String[] args) {
// test new Resource
System.out.println("test new Resource");
try (ReadFile readFile = new ReadFile();) {
readFile.printContentOfFile("Test.txt");
} catch (Exception e) {
System.out.println();
}
}
Result:
test new Resource
Try With Resource line 1
Try With Resource line 1
Try With Resource line 2
Try With Resource line 3
Try With Resource line 4
Try With Resource line 5
Close Read File
When the point jump out try-with-resource statement, the resource auto close ReadFile resource. We don't need to manually close resource.