Skip to main content
Filter by
Sorted by
Tagged with
1 vote
0 answers
103 views

Can the JVM poll for asynchronous exceptions just before entering a try-block?

Recently I read about JLS 11.1.3. Asynchronous Exceptions. I was wondering if there are any specifications about where polling for asynchronous exceptions (and throwing them) can or cannot happen. ...
stonar96's user avatar
  • 1,452
2 votes
2 answers
123 views

Nested try-finally blocks, equivalence and guarantees in Java

Recently I was working on a "cleanup" method and I wanted to make sure that each statement is attempted to be executed. My first approach was something like this (where s#; are statements): ...
stonar96's user avatar
  • 1,452
0 votes
0 answers
96 views

C# try finally works differently on different computers [duplicate]

I am trying to run this code in console application to understand how exactly works try-finally try { Console.WriteLine("Message"); throw new Exception("Error"); } finally {...
sharpflexer's user avatar
0 votes
1 answer
129 views

Behaviour of try finally with a generator

I'm following the Effective Python course by Brett Slatkin on O'Reilly: https://learning.oreilly.com/videos/effective-python/9780134175249/ I've come across the following code snippet while learning ...
Lak's user avatar
  • 35
1 vote
2 answers
600 views

What does "finally: pass" do in Python? (e.g. in try – except – finally)

I would have assumed a finally clause with only a pass to be pointless. But in a Bottle template, the following code for an optional include would not work without it. The result would contain ...
Watchduck's user avatar
  • 1,146
-2 votes
1 answer
708 views

When does the '.finally' block run in async function? [duplicate]

So I have a function that encloses an async computation to ensure at the end of the computation, something happens (like releasing certain resources). so the function goes like const withinScope = ...
caeus's user avatar
  • 3,564
0 votes
0 answers
46 views

Why the finally section is not executed in this official C# code? [duplicate]

This example took from the official documentation does not behave as it says (does not execute the finally) because it just prints: Unhandled exception. System.InvalidCastException: Unable to cast ...
user33276346's user avatar
  • 1,659
0 votes
1 answer
49 views

How to still execute finally block when the code is stopped through Task Scheduler?

I have this code example: import time from datetime import datetime def log_info(message: str): with open('somefile.log', 'a') as file: file.write(f'{datetime.now()}: {message}\n') try: ...
Marty's user avatar
  • 19
0 votes
3 answers
174 views

Execution order with try-finally in python generator functions [duplicate]

def gen(): try: yield 1 finally: print("finally") def main(): print(next(gen())) This code prints finally 1 I don't understand the order of execution here. Why ...
wiseacre's user avatar
0 votes
1 answer
111 views

PHP working after Fatal Error in the include file. Error disappear in the try-finally-return

index.php <?php function bar() { try { $res = 'loading foo:'; include 'foo.php'; } finally { return $res . 'finally.'; } } $res = bar() . " Why it's ...
Ivan Kurziukov's user avatar
0 votes
0 answers
142 views

Spring Boot : How to httpSession.invalidate() after return statement?

I am working on internationalization in Spring boot. Is it possible to httpSession.invalidate() after returning a page. As an example try { return "finalPage"; }...
okan's user avatar
  • 91
3 votes
2 answers
132 views

Do Ada 83 exceptions include resource cleanup?

Ada 83 was one of the first languages to have exceptions. (I want to say 'the first', but one thing I have learned from looking into the history of technology is that there is almost always an earlier ...
rwallace's user avatar
  • 32.9k
3 votes
1 answer
1k views

Is try-finally being deprecated with Java 18+?

JEP 421, being released in the forthcoming Java 18, deprecates finalization. I understand this mean the finalize() method is being deprecated. However, it also mentions the try/finally block and ...
k314159's user avatar
  • 9,547
0 votes
1 answer
36 views

How to return try block value in java even before finally block start executing?

I have code snippet in java like Int methodHitByAPI() { List returnValue = doSomething(); return returnValue; finishProcess(returnValue); } My doubt is that i wanted to execute ...
Indra Neel's user avatar
-1 votes
2 answers
825 views

How to call a method in try with resources [closed]

I was using try..finally code but I want to use try with resource but I am not sure how to call a method in try with resource can anyone help me with that? using try finally try{ } catch{} finally{ //...
bourbon2756's user avatar
2 votes
2 answers
576 views

Difference between code in JavaScript finally block and after try ... catch [duplicate]

This question may appear similar to others that have been answered, but I can't find an answer relating to JavaScript. What is the difference between code in a finally block and code that comes after ...
Ben Zelnick's user avatar
3 votes
1 answer
189 views

Why does returning a variable in a try block not change the value of the returned thing when this variable is reset in the finally section? [duplicate]

I don't really understand how the instructions flow in the following code. The body of finally is guaranteed to be executed before the method returns. If so, the return value should be 0 rather than 1....
Display Name's user avatar
  • 14.9k
0 votes
0 answers
71 views

Java problem with Scanner input and try {/// } finally {input.close();}

Java newbie here... I want to scan some user input after a button is pressed. The first thing that I scan from keyboard works fine but in the second input the programm crashes. I believe the problem ...
Stelios Kapsis's user avatar
1 vote
2 answers
132 views

Do you need a catch block if all you're doing is throwing the caught exception?

I found some code where they want to propagate an exception, but they want to run some clean-up code beforehand, so naturally it uses Try/Catch/Finally. But... they aren't actually doing anything ...
Mark A. Donohoe's user avatar
0 votes
1 answer
831 views

finally block does not execute if exception is thrown [duplicate]

finally block would execute if an exception is thrown in try block here: public class ExceptionTest{ public static void main(String args[]) { System.out.println(print()); } public static int ...
Amani's user avatar
  • 11
0 votes
2 answers
264 views

Try-finally block with return doesn't rise exception

In this code main method's Catch doesn't catch Runtime exception. after the execution of finally block, it should've gone to the exception block of main, but it doesn't. class FinallyDemo { ...
Antar Roy's user avatar
1 vote
0 answers
111 views

correctly closing input stream chains

I'm reading a file containing binary data followed by a serialized object: FileInputStream fis = new FileInputStream(file); GZIPInputStream gzis = new GZIPInputStream(fis); DataInputStream dis = new ...
DutChen18's user avatar
  • 1,165
22 votes
1 answer
30k views

Using "try"+"finally" without "except" never generates any error [duplicate]

I thought that if I use "try" and just "finally" else, without any "except", if the "try" statements couldn't be executed, the "finally" statements should be executed, but after that, an error should ...
Ali Rojas's user avatar
  • 609
-1 votes
2 answers
111 views

Why does a variable need to be static in the finally-block [duplicate]

I use Eclipse for programming and it tells me if want to output the "Input String" Cannot make a static reference to the non-static field Input Why has the variable to be static in the finally-...
Destro's user avatar
  • 27
0 votes
0 answers
118 views

Are there guarantees in C# that the using statement won`t inherit the try + finally combinations issues? [duplicate]

Are there guarantees in C# that the using statement won`t inherit the try + finally combinations issues? The question naturally follows the discussion from other here. According to the documentation:...
qqqqqqq's user avatar
  • 2,149
3 votes
3 answers
5k views

Why finally block may not execute when exception is thrown?

For a long time I thought that it allows me to free up all the resources in the finally block and I thought that if an exception happens in the try block, then the resources will still be free up in ...
qqqqqqq's user avatar
  • 2,149
0 votes
2 answers
182 views

Why does both except and finally block execute here? [duplicate]

from sys import exit try: print("try") exit(0) except: print("except") finally: print("finally") The program should have terminated in exit(0), isn't it? Why was its output : try except ...
prabesh013's user avatar
0 votes
1 answer
601 views

alternative to try finally for logging in Java [closed]

Many methods in my code I am currently working on follows a certain pattern: public void aMethod(...) throws CustomException { Log("aMethod Started") try { //Many method calls that ...
Birgen Vermang's user avatar
2 votes
1 answer
589 views

What could be practical examples of using finally in the try block in Python [duplicate]

When does it make sense to use finally in the try..except block? Isn't listing statements just after the try..except doing the same? What's the difference between these two? try: result = 100 / ...
Aidas Bendoraitis's user avatar
8 votes
3 answers
619 views

Is it an error to return a value in a finally clause [duplicate]

If I try the following code, I see that the normal block return value is not returned, but the finally block return value is: >>> def f(): ... try: ... return "normal" ... ...
Laurent LAPORTE's user avatar
0 votes
0 answers
460 views

When should I use Throwable.getSuppressed() and addSuppressed() in my code?

I'm practicing with try-with-resources for my methods to understand that. This is my example method. public void a(File f) throws FileNotFoundException, IOException { try (FileInputStream fis = ...
user11611653's user avatar
3 votes
3 answers
952 views

in C# try-finally how to catch the original exception

My simple example is: void FixedUnalterableMethod() { try { throw new Exception("Exception 1"); //line 12. } finally { throw ...
Cyrus's user avatar
  • 2,389
2 votes
0 answers
33 views

Return statement in `finally` overrides return statement in `try` [duplicate]

I've run into some problems in my code, and so I have narrowed it down to this simple use-case: function func() { try { return true; } finally { return false; } } ...
goodvibration's user avatar
0 votes
1 answer
183 views

In what condition print('done') statement will execute and will not execute?

I'm bit confused that in which case does the finally block along with the print('done') statement will execute. Because when the try block will execute then only the finally block executes along with ...
coder9A's user avatar
4 votes
3 answers
311 views

How to use try-finally construction in C#? [closed]

We've seen many questions about try-catch-finally and try-finally constructions on this forum. The number of answers increases the number of questions, so I have few too. Here's a link into ...
Oleg Yanytskii's user avatar
0 votes
0 answers
70 views

Java know in finally that exception thrown without any variable [duplicate]

Is there a way to know in finally that exception is thrown, without using any extra variable as below: boolean hasFailed = true; try { // code //... hasFailed = false; } finally { ...
Ori Marko's user avatar
  • 58.3k
0 votes
0 answers
511 views

Python "finally" clause doesn't execute if "try" clause throws SyntaxError [duplicate]

Consider the following code: try: a = [] a[4] = 4 except ZeroDivisionError: print('h') finally: print('y') The output shows that the finally clause was executed: y Traceback (most ...
Henry Barker's user avatar
0 votes
1 answer
280 views

Finally block overriding in java [duplicate]

Given below is my example about a simple finally block. public class Ex2 { public static void main(String args[]) throws ArithmeticException { System.out.println(Ex2.myTestingFuncn()); ...
Dil.'s user avatar
  • 2,066
1 vote
1 answer
202 views

Doing clean-up inside finally block, with return in try block - bad practice?

I have this code to figure out the width of some text: function textWidth(text, font) { const span = document.createElement('span') try { span.style.cssText = `position: absolute; ...
Magnus Buvarp's user avatar
2 votes
2 answers
378 views

Using try-finally to execute statements after return

Consider the following code: Foo result = array[index]; index = (index + 1) % array.length; return result; In order to perform some final actions an extra variable is required. Does it make sense to ...
OLEGSHA's user avatar
  • 450
2 votes
0 answers
249 views

Scanner Resource Leak

Today I have written a Java code that reads input from 2 txt files named input1.txt and input2.txt and solves 2 programming problems. It runs from the terminal like: java berkSol input1.txt input2....
BUY's user avatar
  • 736
0 votes
0 answers
16 views

Breaking out of finally statement without interfering with return value in try statement

Alright, so i have a situation where there is a sequence which has to be executed in particular order inside try statement, if any of the steps fails, return value has to be returned. Also there is a ...
Sir DrinksCoffeeALot's user avatar
47 votes
6 answers
15k views

How to determine if an exception was raised once you're in the finally block?

Is it possible to tell if there was an exception once you're in the finally clause? Something like: try: funky code finally: if ???: print('the funky code raised') I'm looking to ...
wim's user avatar
  • 357k
1 vote
2 answers
128 views

Is it a good/right practice to use Finally keyword to prevent the JVM from re-ordering?

I just think of this question, and I have not found a similar question asked anywhere. I can close this one if there is a duplicate that I have not found. Here is a simple example: Assuming this is a ...
Lubor's user avatar
  • 1,009
7 votes
2 answers
10k views

Using try-finally block inside while loop [duplicate]

I am trying to understand the mechanism when i use finally inside a while loop. In the below code. In finally line prints and than the while breaks. I was expecting the code not to reach the finally ...
rematnarab's user avatar
  • 1,305
5 votes
1 answer
627 views

What is wrong with this Java Puzzlers piece of code?

In new, third edition of Effective Java Joshua Bloch mentions piece of code from Java Puzzlers (it's about closing resources in try-finally): For starters, I got it wrong on page 88 of Java ...
ctomek's user avatar
  • 1,766
4 votes
0 answers
442 views

Why is a finally block *sometimes* not executed on ThreadAbortException if it contains an await?

UPDATE: I don't think this question is a duplicate of Can ThreadAbortException skip finally? because (1) I'm not creating another thread, so there's no possibility of a race condition, and (2) this ...
Michael Liu's user avatar
0 votes
2 answers
9k views

Execute code after return

On my web API, I want to delete a folder after my return statement. public string Post(HttpRequestMessage request) { //Do cool stuff with request try { return "10.0.2.2:8080/...
BeGreen's user avatar
  • 869
4 votes
1 answer
702 views

Is there any purpose for an empty Try/Finally block?

I have inherited a large code base that is full of constructs like this: try DoWhatever; finally end; Sometimes "DoWhatever" involves some ...
user3810626's user avatar
  • 7,489
1 vote
0 answers
40 views

Awareness of exception in finally block, but without catch block [duplicate]

Is it possible to determine if an exception was thrown without a catch block? Visual Studio is able to mine the exception out of my program state and put it as a pseudo-variable ($exception). Can I do ...
Jamaic's user avatar
  • 11