B4J Question Return inside Try Catch

aeric

Expert
Licensed User
Refer to previous thread:

B4X:
Dim sql As SQL = pool.GetConnection
Try
 'work with sql
 
 If some_condition = True Then
  Return
 End If
 
Catch
 'handle failure
End Try
sql.Close

If the code exit from "Return", does the code after "End Try" execute ? In the above example, sql.Close.
 

emexes

Expert
Licensed User
If the code exit from "Return", does the code after "End Try" execute ?
No.

But if you still really want to try this quantum duality approach to programming, you could use Sleep(0) instead of Return to harness the magic of resumable subs.
 
Upvote 0

aeric

Expert
Licensed User
I want to make sure the sql connection is closed otherwise it will cause connection time out between the client and server.
 
Upvote 0

aeric

Expert
Licensed User
Meaning everytime I need to close the connection before using Return?

B4X:
Sub Foo
    Dim sql As SQL = pool.GetConnection
    Try 
        If some_condition = True Then
            sql.Close
            Return
       End If

      'work with sql 
    Catch
     'handle failure
    End Try
    sql.Close
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Why not just do
B4X:
Sub Foo
    Dim sql As SQL = pool.GetConnection
    Try
        If some_condition = False Then
            ‘Do your processing
                'work with sql
       End If

    Catch
     'handle failure
    End Try
    sql.Close
End Sub
No extra return needed that complicates everything

edit: fixed semantics of code
edit2: should not be on my phone
 
Upvote 0

aeric

Expert
Licensed User
Why not just do
B4X:
Sub Foo
    Dim sql As SQL = pool.GetConnection
    Try
        If some_condition = False Then
            ‘Do your processing
                'work with sql
       End If

      'work with sql
    Catch
     'handle failure
    End Try
    sql.Close
End Sub
No extra return needed that complicates everything

edit: fixed semantics of code
Ok, I will try to avoid using Return. I am converting my PHP script which use return; so I also use the same approach in B4J.
 
Upvote 0

OliverA

Expert
Licensed User
Btw, you may want to move the getconnection into the try part. It will fail if there is an issue.
 
Upvote 0

aeric

Expert
Licensed User
Btw, you may want to move the getconnection into the try part. It will fail if there is an issue.
Ok, I will experiment with it later. For the time being, everything seems working fine.
 
Upvote 0
Top