Can not use return in a finally block Topic is solved

Report problems with documented functionality
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Can not use return in a finally block

19 Jul 2023, 07:05

Code:

Code: Select all

f(){
	try{
	}finally{
		return '2'
	}
}
f()
Cannot use return in a finally block. And it will throw like that:
image.png
image.png (21.94 KiB) Viewed 831 times
It seems counterintuitive, as every other language supports return in finally, even when func already returned in try/catch
iseahound
Posts: 1448
Joined: 13 Aug 2016, 21:04
Contact:

Re: Can not use return in a finally block

19 Jul 2023, 23:08

What do you gain from this?
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Re: Can not use return in a finally block

20 Jul 2023, 00:37

iseahound wrote:
19 Jul 2023, 23:08
What do you gain from this?
Em...
Just Return value after a catch or try. :| It's useful, isn't it?
User avatar
lmstearn
Posts: 695
Joined: 11 Aug 2016, 02:32
Contact:

Re: Can not use return in a finally block

20 Jul 2023, 01:53

The behaviour of Try isn't documented when there is no statement inside the following block, did you mean to have one?
Docs wrote:Goto, Break, Continue and Return cannot be used to exit a Finally block, as that would require suppressing any control flow statements within the Try block. For example, if Try uses return 42, the value 42 is returned after the Finally block executes. Attempts to jump out of a Finally block using one of these statements are detected as errors at load time where possible, or at run time otherwise.
Exit can be used like Return in this case:

Code: Select all

f(){
	try{
	   if Mod(A_Min, 2)
		throw Error("That's odd...")
	}finally{
		exit 2
	}
}
f() 
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
lexikos
Posts: 9593
Joined: 30 Sep 2013, 04:07
Contact:

Re: Can not use return in a finally block  Topic is solved

20 Jul 2023, 02:07

That is clearly by design. The error message is telling you that you cannot do it. The documentation tells you that you cannot do it. Yet you post a "bug report".
as every other language supports return in finally
Wrong. C# does not - I would bet that the other .NET languages do not either.
iseahound
Posts: 1448
Joined: 13 Aug 2016, 21:04
Contact:

Re: Can not use return in a finally block

20 Jul 2023, 07:24

I think they mean that any return in a finally block would override an existing return in either try/catch. I'm just curious as to your use case, it seems that the pattern shown here:

Code: Select all

f() { ; Note this function returns 3. 
   try return 2
   finally
      return 3
}
could be done via assignment:

Code: Select all

f() { ; Note this function returns 3. 
   try result := 2
   finally
      result := 3
   return result
}
It's really interesting that a return in a finally block can override an existing return in try / catch but ultimately I haven't seen any examples in other languages where its usage is convincing.
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Re: Can not use return in a finally block

20 Jul 2023, 07:39

I have accepted it now because they have told me it has been documented.
By the way, it's not exactly the same. Because return result outside the finally block will not run if an exception is caught:
iseahound wrote:
20 Jul 2023, 07:24

Code: Select all

f() { ; Note this function returns 3. 
   try return 2
   finally
      return 3
}
could be done via assignment:

Code: Select all

f() { ; Note this function returns 3. 
   try result := 2
   finally
      result := 3
   return result
}
I'm having trouble understanding how much better it is to allow a return in a finally block, as I haven't seen any examples online in other languages where its usage is convincing.
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Re: Can not use return in a finally block

22 Jul 2023, 10:15

@iseahound
Because return result outside the finally block will not run if an exception is caught:
Oh! Sorry, I suddenly find I have mistaken it🤣. It will run instead. You are ritght. :+1:
iseahound
Posts: 1448
Joined: 13 Aug 2016, 21:04
Contact:

Re: Can not use return in a finally block

22 Jul 2023, 13:59

No worries. You just taught me that return could be in a finally block, and I was curious to see how it is supposed to be used!

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 14 guests