
The finally clause in the try-catch exeception block always executes, irrespective of the occurence of exeception. This is applicable for the normal java program flow. If the execution flow is stopped irreversibly before the finally clause, then the finally block will not be executed.
How can the user achieve that in Java?
Include “System.exit(1);” before the finally block and stop the execution flow of the java program.
Comments are closed for "When does the finally clause in java exception block never executes?".
Is there any example situation to avoid finally block and stop the JVM by System.exit(n)?
can i use finalize or finalization
well said
i didnt know it before
i use all the clause e.g try ,catch, throw,throws & finally…… Finally is always be executed i agree with that… but what i mention in try block that’s why finally blk not executed…..
Plz… guide me….
finalize or finalisation cannot be used ismail….their functionality is different
could you please give the difference between Final Keyword and Private access modifier?
@Nagarjuna
private members can be accessed within the class only. You will get compilation otherwise.
But In case of Final
1. Final Variables can not be modified after they have been initialized.
2. Final class can not be extended.
3. Final methods can not be overridden.
Final variables can be marked private and vice-a-versa.
Hope this solves your doubts.
which will effect if i keep init() method explicitly in jsp? and how it will execute?
Thanks for the reply i was asked this question in a interview
Sir I want to tell that finally block will not execute when the finally block itself have an exception ,Or the reason u mentioned
Can a finally be placed before a catch statement??
finalize or finalisation cannot be used ismail……finalize use in garbage collection.
can we give any literal to the exit(args) method
Hey Joe U didn’t mention where to add this System.out(1). means in try block or catch block???
there is also a concept of Daemon thread to stop finally block or to prevent finally from execution. Do any1 know dis?
Final is keyword that can be used with the variable, method and class.
Following are the usage of Final Keyword.
EX 1:
final int age=30;
Cannot change the value of the age. Its like a constant.
EX : 2
public final void display(){
//Your code goes here.
}
Cannot override the method in the sub-class.
EX : 3
final class Test {
// Member Variable and methods
}
Cannot extend this class
2. Private is a access specifier and it applies to variable, method and classes.
Variable – we can use only with in the class but we can change the value of the variable.
Method – We can use only with in the class, cannot access outside the class and so on.
Hope this answers your question
Thanks
Ashok
to clarify, Final Variables cannot be reassigned, they can be modified.
final Map m = new HashMap();
m.add(“one”, 1); // this is modification
Map n = new HashMap();
m = n; // this is reassignment and not allowed for final variables.
final String s = “one”; // initialization
String t = “two”
s = t; // reassignment, not allowed
t = “three” // non-final reassignment, allowed
final int i = 0;
i = 1; // reassignment, not allowed.
Another way to say it is that final variables may not be modified by reassignment, however, the internal object state may be changed (modified).
Hope that helps.
oops, should have said m.put(“one”, 1) rather than m.add… sorry for that confusion
Finally block is always executed except some of the following cases.
If the JVM exits while the try or catch code is being executed, then the finally block may not execute.
e.g. System.exit(0);
If the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
e.g. Thread.interrupted();
what happen when finally block throw an exception ?
Nice post..!
when the finally clause in not executed?
A).system.exit(0);
This is one way.is there any other way not to execute finally block.