If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
पिछले topic में आपने Java में try catch के बारे में पढ़ा , try catch का use करके Exception को handle किया। इस topic में हम finally block के बारे में पढ़ेंगे।
Exceptions को handle करने के लिए हम अपना code try{ }
block के अंदर लिखते थे , अगर error आती थी तो उस error को catch block में handle करते थे।
कई बार ऐसी situation आती है कि code को दोनों conditions में Run करना हो , means Exceptions आये तब भी code Run हो और न आये तब भी, वहाँ पर हम finally{ }
block use करते हैं।
●●●
finally{ }
block , try-catch के बाद हमेशा run होता है।
अगर कोई Exceptions नहीं है तो try{ }
block के बाद run होगा
और Exceptions आयी तो catch()
block के बाद।
public class Test {
public static void main(String[] args) {
// now use try catch finally block.
try {
int result = 10/0;
}
catch(ArithmeticException error) {
System.out.println("Error occurred : "+ error.getMessage());
}
finally {
System.out.println("finally block is running");
}
System.out.println("rest of the code is runnning...");
}
}
Output
Error occurred : / by zero finally block is running rest of the code is runnning...
ऊपर दिए गए example में error थी इसलिए catch block execute होने के बाद finally{ }
block execute हुआ है , suppose अगर कोई error नहीं भी आती तो finally block try{ }
block के बाद execute होता है।
For example
public class Test {
public static void main(String[] args) {
try {
int result = 10/2;
}
catch(ArithmeticException error) {
System.out.println("Error occurred : "+ error.getMessage());
}
finally {
System.out.println("finally block is running");
}
System.out.println("rest of the code is runnning...");
}
}
Output
finally block is running rest of the code is runnning...
आप example में देख सकते हैं कि कोई exception न होने पर सिर्फ try
और finally{ }
block ही run हुए हैं।
●●●
Yes , आप catch()
block को skip करके directly try block के साथ finally{ }
block execute करा सकते हैं।
public class Test {
public static void main(String[] args) {
try {
System.out.println("try block is runnning...");
}
finally {
System.out.println("finally block is running...");
}
}
}
Output
try block is runnning... finally block is running...
ध्यान रहे कि finally{ } , catch() के बाद ही use होगा। हाँ अगर आप catch() use नहीं कर रहे हैं तो finally{ } को try{ } के बाद use कर सकते हैं।
finally{ }
को catch()
से पहले use करने पर error आएगी।
public class Test {
public static void main(String[] args) {
// now use try catch finally block.
try {
int result = 10/0;
}
finally {
System.out.println("finally block is running");
}
catch(ArithmeticException error) {
System.out.println("Error occurred : "+ error.getMessage());
}
}
}
Test.java:10: error: 'catch' without 'try' catch(ArithmeticException error) { ^ 1 error
●●●
I Hope, अब आपको Java में finally{ }
block अच्छे से समझ आ गया होगा।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook