Does this finally get executed ?

G

GIMME

Psuedo code ...

Does sc.close() get executed ?

public static Employee get(int id ) throws Exception
{
try {
SomeConnection sc = new SomeConnection();
return new Employee();
}finally {
sc.close();
}
 
T

Tony Morris

GIMME said:
Psuedo code ...

Does sc.close() get executed ?

public static Employee get(int id ) throws Exception
{
try {
SomeConnection sc = new SomeConnection();
return new Employee();
}finally {
sc.close();
}

No. It won't compile.
Nothing is executed.
 
V

VisionSet

GIMME said:
Psuedo code ...

Does sc.close() get executed ?

public static Employee get(int id ) throws Exception
{
try {
SomeConnection sc = new SomeConnection();
return new Employee();
}finally {
sc.close();
}

Yes, finally is always executed.
I think the only exception is calling System.exit(0)
 
T

Tony Morris

Yes, finally is always executed.
I think the only exception is calling System.exit(0)

There are other exceptions:
- throwing an exception from within a finally block.
- explicitly returning from within a finally block.
Both of these should be avoided as a matter of form.

Note, however, that the given example will not compile due to a scope
problem.
 
V

VisionSet

It won't compile.
There is a scope error.
Nothing is executed.

I think scoping issues may be arguably left out of pseudocode.
Not that I'm advocating sloppy questions, even though I'm probably guilty of
them myself a fair bit.
 
C

Chris Smith

(e-mail address removed) says...
There are other exceptions:
- throwing an exception from within a finally block.
- explicitly returning from within a finally block.
Both of these should be avoided as a matter of form.

Neither of these cases will prevent a finally block from executing.
They might, however, prevent it from completing normally and therefore
cause an exception to be ignored; perhaps that's what you're thinking
of.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
L

Lasse Reichstein Nielsen

Tony Morris said:
There are other exceptions:
- throwing an exception from within a finally block.
- explicitly returning from within a finally block.

In both cases, the finally block *is* executed. It's just
that perhaps not all of it is, but that's not much different
from:
try {
...
} finally {
if (false) { ... }
}

/L
 
T

Tony Morris

I apologise for the ambiguity.
This is what I meant.

By "executing a finally block", I meant executing from start to end as is
often (should be) intended.
 
T

Tony Morris

In both cases, the finally block *is* executed. It's just
that perhaps not all of it is, but that's not much different
from:
try {
...
} finally {
if (false) { ... }
}

Not quite.
There is a difference.
Generally, one is very nasty (returning or throwing from a finally); and one
isn't (an if construct in a finally block - perfectly legitimate).
Google appears to have thousands of explanations.
 
A

Ann

GIMME said:
Psuedo code ...

Does sc.close() get executed ?

public static Employee get(int id ) throws Exception
{
try {
SomeConnection sc = new SomeConnection();
return new Employee();
}finally {
sc.close();
}

Maybe, if "SomeConnection();"
has an exception it will
 
T

Tor Iver Wilhelmsen

VisionSet said:
I think scoping issues may be arguably left out of pseudocode.

If it's pseudocode it's not Java, and as such we cannot answer because
try/finally might do something other in that pseudocode language.

IF the code was rewritten to actually compile, then the try block
would execute in the example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top