Does Return also Exit Sub/Func?

M

mp

given a try block like this
Function Something()As Object
....some code
Try
DoSomething

Catch ex As Exception
'if failed
Return Nothing
'is that the same as Exit Function?
End Try
....more code
Return someObject
End Function

if the exception occurs, does the Return Nothing also exit the function (so
....more code won't run)
or do i also need to add Exit Function if i don't want 'more code' to run?

thnaks
mark
 
D

Dennis

given a try block like this
Function Something()As Object
...some code
Try
DoSomething

Catch ex As Exception
'if failed
Return Nothing
'is that the same as Exit Function?
End Try
...more code
Return someObject
End Function

if the exception occurs, does the Return Nothing also exit the function (so
...more code won't run)
or do i also need to add Exit Function if i don't want 'more code' to run?

Have you traced it in the debugger to see what happens?
 
A

Armin Zingler

Am 15.07.2010 16:45, schrieb mp:
given a try block like this
Function Something()As Object
...some code
Try
DoSomething

Catch ex As Exception
'if failed
Return Nothing
'is that the same as Exit Function?
End Try
...more code
Return someObject
End Function

if the exception occurs, does the Return Nothing also exit the function (so
...more code won't run)
or do i also need to add Exit Function if i don't want 'more code' to run?

You could "try" this also. :)

Return exits the function. "Return Nothing" is equal to

Something = Nothing
Exit Function

(
you know that you can set the return value by setting the invisible
variable like in

function whatever as string
whatever = "test"
end function
)


Though, if Return is in a Try-Block with a Finally part, the finally part
is always executed:

try
return
catch
finally
msgbox "i'm here"
end try

msgbox "you don't see me"

You'll see the 1st Msgbox. (that's the purpose of finally; it's always
executed)
 
M

mp

Armin Zingler said:
Am 15.07.2010 16:45, schrieb mp:

You could "try" this also. :)

Return exits the function. "Return Nothing" is equal to

Something = Nothing
Exit Function

(
you know that you can set the return value by setting the invisible
variable like in

function whatever as string
whatever = "test"
end function
)


Though, if Return is in a Try-Block with a Finally part, the finally part
is always executed:

try
return
catch
finally
msgbox "i'm here"
end try

msgbox "you don't see me"

You'll see the 1st Msgbox. (that's the purpose of finally; it's always
executed)

thanks to all
yes, i'll try first next time
sorry
mark
 
J

J.B. Moreno

mp said:
given a try block like this
Function Something()As Object
...some code
Try
DoSomething

Catch ex As Exception
'if failed
Return Nothing
'is that the same as Exit Function?
End Try
...more code
Return someObject
End Function

if the exception occurs, does the Return Nothing also exit the function (so
...more code won't run)
or do i also need to add Exit Function if i don't want 'more code' to run?

Yes it does. But if memory serves, neither Return nor Exit will keep a
Finally block from running. And a Return value in there could override
one done elsewhere.


Function Y () as Integer

Try
return 1
Catch ex As Exception
return 2
Finally
return -1
End Try

return 0
End Function


Should always return -1.
 

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

Top