squeakyloboy
asked on
What to return from a function if an exception is thrown?
Hi,
I have a function that I call to create an arrayList of objects. I've enclosed it in a 'Try...Catch' block. I return the arrayList at the end of the 'Try' and in the 'Catch' I display a messagebox with whatever the exception is in it's message.
I'm getting a warning that 'Function blah doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.'
So, I'm just wondering what I'm supposed to do to get rid of this warning? Do I return an empty arrayList and then test for that in the function call? Or is there a better solution?
Thanks,
Chris
I have a function that I call to create an arrayList of objects. I've enclosed it in a 'Try...Catch' block. I return the arrayList at the end of the 'Try' and in the 'Catch' I display a messagebox with whatever the exception is in it's message.
I'm getting a warning that 'Function blah doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.'
So, I'm just wondering what I'm supposed to do to get rid of this warning? Do I return an empty arrayList and then test for that in the function call? Or is there a better solution?
Thanks,
Chris
ASKER
Hi bruintje, thanks for the reply.
Ok, I've moved the return as follows:
Finally
Return tracksList
End Try
...but I get the error 'Branching out of a 'Finally' is not valid'.
Any ideas?
Ok, I've moved the return as follows:
Finally
Return tracksList
End Try
...but I get the error 'Branching out of a 'Finally' is not valid'.
Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ah, got it. That's kinda what I thought but it's good to confirm it.
Thanks a lot!
Chris
Thanks a lot!
Chris
----------
a way is to add a finally part to the try catch
Function
Try
Catch ex As Exception
Finally
'this will always run so put the return value here
End Try
end function
----------
bruintje
share what you know, learn what you don't