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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. try-catch question ...

try-catch question ...

Scheduled Pinned Locked Moved C#
helptutorialquestion
5 Posts 3 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.
  • H Offline
    H Offline
    Hussam Fattahi
    wrote on last edited by
    #1

    while using try-catch combination, I've assign a value to a reference type variable within try block, but when I tried to use this variable within catch block it throw an exception (NullRefrenceException) or an error (use of unassigned local variable) occur. my question is: the variables that has been declared, or had values assigned for them in try block, how to deal with them in catch block. the code I'm working on : int i =0; Socket remote; try { remote = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); i = 5; } catch(Exception ee) { MessageBox.Show(i.ToString()); MessageBox.Show(remote.ToString()); //the error occured here. } and why is that when i used a value type variable it works well.

    C 1 Reply Last reply
    0
    • H Hussam Fattahi

      while using try-catch combination, I've assign a value to a reference type variable within try block, but when I tried to use this variable within catch block it throw an exception (NullRefrenceException) or an error (use of unassigned local variable) occur. my question is: the variables that has been declared, or had values assigned for them in try block, how to deal with them in catch block. the code I'm working on : int i =0; Socket remote; try { remote = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); i = 5; } catch(Exception ee) { MessageBox.Show(i.ToString()); MessageBox.Show(remote.ToString()); //the error occured here. } and why is that when i used a value type variable it works well.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      remote is going to be null, given that you've had an exception, and it's not likely that i=5 threw it.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      H 1 Reply Last reply
      0
      • C Christian Graus

        remote is going to be null, given that you've had an exception, and it's not likely that i=5 threw it.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        H Offline
        H Offline
        Hussam Fattahi
        wrote on last edited by
        #3

        well, that is my qustion. i need to assign a value to 'remote' in try block, and then use that value within catch block. is there any direct way, or trick to do so.

        C S 2 Replies Last reply
        0
        • H Hussam Fattahi

          well, that is my qustion. i need to assign a value to 'remote' in try block, and then use that value within catch block. is there any direct way, or trick to do so.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Yes, make a call to the remote constructor which does not fail. There's no rocket science here. It's null going in, your attempt to assign a value is failing, and so it's still null. The fault is in your code, it needs to not make assumptions about things that may not have happened, because you were in a try block.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          1 Reply Last reply
          0
          • H Hussam Fattahi

            well, that is my qustion. i need to assign a value to 'remote' in try block, and then use that value within catch block. is there any direct way, or trick to do so.

            S Offline
            S Offline
            Scott Dorman
            wrote on last edited by
            #5

            Hussam Fattahi wrote:

            i need to assign a value to 'remote' in try block, and then use that value within catch block.

            Usually the only things you would need to do in the catch block are cleanup type activities. If you are trying to show the error message to the user you should be doing something like this:

            int i =0;
            Socket remote = null;
            try
            {
            remote = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            i = 5;
            }
            catch(Exception ee)
            {
            MessageBox.Show(i.ToString());
            MessageBox.Show(ee.Message);
            }

            The Socket remote **= null**; will remove the compiler issue about using an unassigned variable. Any variable that is used inside of a catch block must be explicitly assigned prior to it being used. If the call to remote = new Socket(...) fails for any reason, remote will be null (in all cases), you will take an exception that puts you in your catch handler. At that point, remote is still null, so you are trying to run ToString() on a null reference, which you can't do.

            ----------------------------- In just two days, tomorrow will be yesterday.

            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