forked from akash-coded/C133-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalizeMethod.java
More file actions
38 lines (36 loc) · 1.42 KB
/
FinalizeMethod.java
File metadata and controls
38 lines (36 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class FinalizeMethod {
public static void main(String[] args) throws Throwable {
FinalizeMethod obj = new FinalizeMethod(); // 1st object
obj = null;
// obj.finalize(); // Uncaught checked exception
// obj = new FinalizeMethod(); // 1st object becomes orphan and 2nd object is
// created
// try {
// obj.finalize(); // Uncaught checked exception
// } catch (Throwable e) {
// e.printStackTrace();
// }
System.out.println(obj); // Print 2nd object details
System.gc(); // Ignores all unhandled exceptions in finalize()
System.out.println("Main completed");
// obj = null; // 2nd object becomes orphan
}
void disp() {
System.out.println("My message");
}
@Override
protected void finalize() throws Throwable {
// throw new java.io.IOException();
// System.out.println(10 / 0); // ArithmeticException (unchecked)
try {
throw new java.io.IOException(); // IOException (checked)
// System.out.println(10 / 0); // ArithmeticException (unchecked)
} catch (Exception e) {
System.out.println(e);
// e.printStackTrace(); // Creates an uncaught exception (error)
} finally {
System.out.println("Finally inside finalize");
}
System.out.println("Finalize method of FinalizeMethod class invoked");
}
}