Questions tagged [finally]

Questions related to the finally block in try-catch construct.

278 questions
-1
votes
1 answer

Business scenario for finally block - exception handling

Please tell me the business scenario for 'Finally block' in exception handling means a business scenario where we need finally block
Gaurav
  • 1
  • 3
-1
votes
1 answer

How does Java handle returning references when using finally blocks in Java with regards to GC?

In the scenario below how what happens with regards to GC? I'm pretty sure the reference to "a" will not actually get returned, hence no need to worry about leaving this reference in scope. So pretty much the return of "a" never actually takes place…
newlogic
  • 807
  • 8
  • 25
-1
votes
1 answer

I'm getting irregular output for this program? Why?

Finally example public class FinallyExample { public static void main(String[] args) { new FinallyExample().dothework(); } public void dothework() { Object o=null; for(int i=0;i<=4;i++) …
-1
votes
7 answers

finally clause not always called?

If the code below is run from an IDE or from the command line, and then stopped either: Stopping its execution from eclipse (red button) Pressing CTRL+C (command line) Naturally (by replacing the while(true) loop by a for loop never the finally…
Hey StackExchange
  • 2,057
  • 3
  • 19
  • 35
-1
votes
2 answers

why is exception argument not caught by finally block in python

try: ... except (SomeError) as err: ... else: ... finally: if err: ... This gives an error: 'err not defined'. Because the exception argument - err - is not defined as far as the finally block is concerned. It appears then that…
markling
  • 1,232
  • 1
  • 15
  • 28
-1
votes
5 answers

Difference between final keyword, finally block and finalized method in java throught one good example

Often these keywords confuse me. Can any one tell me exactly what the difference between them is?
Mitul Maheshwari
  • 2,647
  • 4
  • 24
  • 38
-1
votes
1 answer

is try-catch without finally the same as try-catch with finally?

I really don't understand use of finally block... in try-catch block, Whether we use finally or not we can get same run of our code. for example what is difference between these code: try { System.out.println(1/0); } …
Ahmad Vatani
  • 1,630
  • 4
  • 21
  • 34
-2
votes
4 answers

will this finally block execute?

Possible Duplicate: In Java, does return trump finally? I came across a java code snippet in a dao implementation .It returns a List as shown below. After the 'return' statement is executed,the finally block tries to close the session.Will this…
markjason72
  • 1,653
  • 8
  • 27
  • 47
-2
votes
2 answers

catch (Exception e) {} vs finally {}

I am trying to understand and use exceptions. why use finally? Instead of finally I could also do a catch(Exception e){}. This way my program never crashes and the code after the catch is always executed. Additionally I could add an if/else after…
Sud0
  • 55
  • 5
-2
votes
1 answer

Correction to try/finally

I'm trying to close my resultset, connection and statement with a try/finally but Sonar doesn't seem to like it. What mistake am I doing and why aren't they closing? Thanks. public static List findByName(String firstName, String lastName)…
Hywel Griffiths
  • 287
  • 5
  • 16
-2
votes
2 answers

How to catch an exception that was thrown inside a catch clause?

try { throw new SomeException(); } catch (SomeException e) { System.out.println("reached once"); throw e; } catch (Exception e) { System.out.println("reached twice"); } This code only…
ninesalt
  • 4,054
  • 5
  • 35
  • 75
-2
votes
4 answers

what would the output of the program?

the output is coming as Test Passed!!! how the builder return when it is set to null at finally block public class Test { public static void main(String args[]) { System.out.println(new Test().print()); } protected…
Ballu
  • 9
  • 1
-2
votes
2 answers

Assigning null to variable in finally block

The output of the following piece of code is "Test Passed"; can someone explain to me why ? public class Test { public static void main(String args[]) { System.out.println(new Test().print()); } protected StringBuilder print()…
Ballu
  • 9
  • 1
-3
votes
1 answer

Hi , why my try block is skipping and control is going to finally block with out giving any exception?

my code is like below try{ System.out.println("index"+index); System.out.println("Inside getPayloadFromKibana elsatic host" + elasticHost); RestHighLevelClient restClient = new RestHighLevelClient(RestClient …
-3
votes
4 answers

why finally block doesn't execute?

Why finally block of this code does not execute? it only prints 1. Is finally block not execute after System.exit(0); ? void method2() { try { System.out.println("1"); System.exit(0); } finally { …
User3031
  • 19
  • 3
1 2 3
18
19