[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

What to return from a function if an exception is thrown?

Posted on 2006-05-25
4
Medium Priority
?
336 Views
Last Modified: 2011-08-18
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
0
Comment
Question by:squeakyloboy
  • 2
  • 2
4 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 16758969
Hi squeakyloboy,
----------

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
0
 

Author Comment

by:squeakyloboy
ID: 16759000
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?
0
 
LVL 44

Accepted Solution

by:
bruintje earned 1000 total points
ID: 16759050
sorry i should've been more clear

Function
Dim tmpVal as something

    Try
       'do some processsing here
      tmpVal =

    Catch ex As Exception
       'do some handling here

    Finally
       'this will always run so put the return value here
       'test if tmpVal is still nothing then set it to nothing
       'tmpVal = Nothing

    End Try
   
    return tmpVal

end function

in the calling function you can test for nothing as input from the function and handle it accordingly
0
 

Author Comment

by:squeakyloboy
ID: 16759383
Ah, got it. That's kinda what I thought but it's good to confirm it.

Thanks a lot!
Chris
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question