Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. try catch and return value

try catch and return value

Scheduled Pinned Locked Moved C#
debugginghelpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Os Sutrisno
    wrote on last edited by
    #1

    I'm writing a function as below: public string returnString() { try { //open connections return var1; } catch (Exception ex) { //write to trace } finally { //close connections } } I'm getting an error that says the function will not return a value in some execution. Where should I return the value? After finally? X|

    C 1 Reply Last reply
    0
    • O Os Sutrisno

      I'm writing a function as below: public string returnString() { try { //open connections return var1; } catch (Exception ex) { //write to trace } finally { //close connections } } I'm getting an error that says the function will not return a value in some execution. Where should I return the value? After finally? X|

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      If your try fails, it will jump to the catch block. There is no return statement in the catch block therefore, once the error is handled it does not know what to return out of the method. You either need an additional return in the catch block or you need to return something after the finally block.


      My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

      O 1 Reply Last reply
      0
      • C Colin Angus Mackay

        If your try fails, it will jump to the catch block. There is no return statement in the catch block therefore, once the error is handled it does not know what to return out of the method. You either need an additional return in the catch block or you need to return something after the finally block.


        My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

        O Offline
        O Offline
        Os Sutrisno
        wrote on last edited by
        #3

        For example, if I am retrieving data from a DB and returning the dataset. An exception occurs and no data is retrieved. In this case, should I still return the dataset in the catch block\after finally? My initial thought was that I wouldn't return anything if an exception occurs. :(

        C 1 Reply Last reply
        0
        • O Os Sutrisno

          For example, if I am retrieving data from a DB and returning the dataset. An exception occurs and no data is retrieved. In this case, should I still return the dataset in the catch block\after finally? My initial thought was that I wouldn't return anything if an exception occurs. :(

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          You must return something as your method signature tells the caller that you will. If the thing fails and you have nothing then return null;


          My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

          O 1 Reply Last reply
          0
          • C Colin Angus Mackay

            You must return something as your method signature tells the caller that you will. If the thing fails and you have nothing then return null;


            My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

            O Offline
            O Offline
            Os Sutrisno
            wrote on last edited by
            #5

            I see! Thanks very much Colin! Regards, Os

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups