Java का finally Block
In this page:
try {
// code
} catch (ExceptionType e) {
// handle
} finally {
// always runs
}
What is the finally Block?
finally block try/catch के बाद चलता है चाहे एक exception throw हुआ हो, caught हुआ हो, या बिल्कुल न हुआ हो। यह specifically उस cleanup code के लिए मौजूद है जिसे चाहे कुछ भी हो बिल्कुल execute होना ही चाहिए।
उदाहरण: What is the finally Block?
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Try code that might throw an exception
try {
// Print a line to the console
System.out.println("try");
// Always runs, whether or not an exception occurred
} finally {
// Print a line to the console
System.out.println("finally always runs");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Cleaning Up Resources
Files, database connections, या network sockets बंद करना finally का classic उपयोग है — आप try से पहले resource खोलते हैं, और finally में release करते हैं ताकि एक leak न हो सके भले ही एक exception operation के बीच में interrupt करे।
उदाहरण: Cleaning Up Resources
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FileWriter writer = new FileWriter("data.txt");
try {
writer.write("Hello");
} finally {
writer.close(); // resource released no matter what
}
}
}
Login to try C/C++/Java/PHP code in the editor
finally with Uncaught Exceptions
अगर एक exception किसी matching catch block से caught नहीं होता, finally अब भी चलता है इससे पहले कि exception caller तक propagate हो। यह गारंटी देता है कि cleanup उन errors के लिए भी होता है जिनकी आपके code ने उम्मीद नहीं की थी।
उदाहरण: finally with Uncaught Exceptions
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Try code that might throw an exception
try {
method();
// Handle RuntimeException if it was thrown
} catch (RuntimeException e) {
// Print a line to the console
System.out.println("Caught in main");
}
}
static void method() {
// Try code that might throw an exception
try {
// Throw RuntimeException to signal an error
throw new RuntimeException("boom");
// Always runs, whether or not an exception occurred
} finally {
System.out.println("finally runs before propagating");
}
}
}
Login to try C/C++/Java/PHP code in the editor
finally vs Return Statements
finally के अंदर एक return चुपचाप try या catch से एक return override करता है, जो भी value वे return करने वाले थे उसे discard करते हुए — एक subtle bug source, इसलिए finally से return करने से बचें जब तक आपका इरादा result replace करने का न हो।
उदाहरण: finally vs Return Statements
public class Main {
static int test() {
try {
return 1;
} finally {
return 2; // silently overrides the try's return
}
}
public static void main(String[] args) {
System.out.println(test());
}
}
Login to try C/C++/Java/PHP code in the editor
When finally Does Not Execute
finally सिर्फ extreme cases में skip होता है: अगर JVM खुद terminate हो (System.exit() से या एक crash से) या अगर इसे चलाने वाली thread mid-execution में forcibly killed हो। लगभग सभी normal control flow के लिए, यह चलने की गारंटी है।
उदाहरण: When finally Does Not Execute
public class Main {
public static void main(String[] args) {
try {
System.out.println("try");
System.exit(0); // finally is skipped after this
} finally {
System.out.println("This never prints");
}
}
}
Login to try C/C++/Java/PHP code in the editor
finallyके अंदरreturnउपयोग करना, जो original exception या return value discard करता है।System.exit()के बादfinallyके चलने की उम्मीद करना, जब यह नहीं चलता।finallyमें ऐसा code रखना जो throw कर सकता है बिना इसे handle किए।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: