Questions related to the finally block in try-catch construct.
Questions tagged [finally]
278 questions
8
votes
3 answers
finally block in daemon thread
I know that finally blocks in deamon threads would not be executed. But my meticulous nature tries to understand why and what happens in JVM so special that it could not call the code under this block.
I think that it somehow related to call stack…

Fedor Skrynnikov
- 5,521
- 4
- 28
- 32
8
votes
6 answers
What is the difference between finally and no finally?
What is the difference between
try {
// action A
}
catch(Exception e) {
// action B
}
finally {
// action C
}
and
try {
// action A
}
catch(Exception e) {
// action B
}
// action C
I have read that you can return from…

Sidor
- 89
- 2
8
votes
2 answers
Should Marshal.FreeHGlobal be placed in a finally block to ensure resources are disposed?
I have the following block of code:
IntPtr unmanagedPointer = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, unmanagedPointer, buffer.Length);
SomeCommandThatCanThrowAnException();
Marshal.FreeHGlobal(unmanagedPointer);
Should the…

vicsz
- 9,552
- 16
- 69
- 101
8
votes
1 answer
How to properly handle ThreadInterruptedException?
public void threadMethod()
{
try
{
// do something
}
catch (ThreadInterruptedException e)
{
Console.WriteLine("[Net]", role, "Thread interrupted.");
n.CloseConnection();
}
finally
{
…

MartyIX
- 27,828
- 29
- 136
- 207
8
votes
7 answers
What is the gist of finally block in Java?
I think on the following examples; but could not figure out what the importance of the finally block is. Can you tell me the difference of the executions of these two code samples? Also a real life example can be helpful.
Sample 1:
try{
…

abuzittin
- 83
- 3
8
votes
1 answer
java skips catch clause jumping straight to finally in apache pdfbox
I'm with my hands in my hair on this one.
I'm using Apache PDFBox, because I want to read pdf files line by line in JAVA and deal with the contents later on. However I have the following problem..
I've used the code below in a seperate java…

Maurits
- 93
- 1
- 4
7
votes
9 answers
In C# will the Finally block be executed in a try, catch, finally if an unhandled exception is thrown?
Another interview question which was expecting a true / false answer and I wasn't too sure.
Duplicate
In .NET, what if something fails in the catch block, will finally always get called?
Does a finally block always run?
Conditions when finally does…

Gais
- 565
- 3
- 10
- 17
7
votes
17 answers
c# "finally" block that only runs on exceptions
Edit: I have looked at the answers code: NONE of them do what I want (I've checked). It would seem that there is no way to do what I want in native c#. I guess that's not a disaster just a shame given that .NET does support it (see accepted…

BCS
- 75,627
- 68
- 187
- 294
7
votes
7 answers
Set reference = null in finally block?
A colleague of mine sets reference to null in finally blocks. I think this is nonsense.
public Something getSomething() {
JDBCConnection jdbc=null;
try {
jdbc=JDBCManager.getConnection(JDBCTypes.MYSQL);
...
}
finally…

deamon
- 89,107
- 111
- 320
- 448
7
votes
4 answers
What's the difference between code inside finally block and code after finally block?
I was wondering what's the difference between code inside finally block and code after finally block

pete
- 1,878
- 2
- 23
- 43
7
votes
3 answers
finally-block and thread suspension
I've noticed that in Java if the current thread is beeing suspended within a try-block the corresponding finally block is not being executed such as in
Semaphore lock = new Semaphore(0);
try {
lock.acquire();
} finally {
// do…

marc wellman
- 5,808
- 5
- 32
- 59
7
votes
5 answers
error while using try with resources in Java
I have this method where I am using try with resources of Java SE 7.
private void generateSecretWord(String filename){
try (FileReader files = new FileReader(filename)){
Scanner input = new Scanner(files);
String…

brain storm
- 30,124
- 69
- 225
- 393
7
votes
4 answers
when does the finally block not execute while try or catch block is interrupted
when does the finally block not execute while try or catch block is interrupted? the doc says that "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…

andy
- 3,951
- 9
- 29
- 40
6
votes
4 answers
Delphi - try finally block is guaranteed by compiler to be executed correctly?
I know this was discussed on other topics also, what I'm asking is exactly the title of this question.
Is there such case when try/finally the finally won't execute?
try
//some error here
finally
//code that MUST be executed
end;
I'm not…

RBA
- 12,337
- 16
- 79
- 126
6
votes
6 answers
Why is my finally block not working in C#?
I've been helping a colleague debug some strange behavior in their code. The following sample illustrates this:
static void Main(string[] args)
{
string answer = Sample();
Console.WriteLine(answer);
}
public static string Sample()
{
…

rbrayb
- 46,440
- 34
- 114
- 174