Try Catch Block

Questions with try catch block

Page 1 of 1

5 Replies - 869 Views - Last Post: 25 July 2010 - 09:11 PM Rate Topic: -----

#1 Guest_ChrisGZF*


Reputation:

Try Catch Block

Posted 25 July 2010 - 08:39 AM

Well, does try catch block somehow makes things private?

Example:
            [b]try[/b]
            [b]{[/b]
                TcpClient [b]client[/b] = worldListener.AcceptTcpClient(); // [b]client[/b] is created
            [b]}[/b]
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.WriteLine("[INFO] " + LoggingFunctions.GetDateTime() + " New connection from " + [b]client[/b].Client.RemoteEndPoint); //give "The name 'client' does not exist in the current context"



but works if the try catch block is removed. Would be helpful if you could give me an explanation and give me a solution.

Is This A Good Question/Topic? 0

Replies To: Try Catch Block

#2 Guest_ChrisGZF*


Reputation:

Re: Try Catch Block

Posted 25 July 2010 - 08:41 AM

Those in [b][//b] are just to highlight stuff and are not in my code. Thanks in advance.
Was This Post Helpful? 0

#3 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Try Catch Block

Posted 25 July 2010 - 08:46 AM

I don't know if "private" is the right word in this case. It's about scope. If you define variables in try { } scope, then they are visible only here, of course. client is defined in that try { } block, therefore you can't access it anywhere else, only the scope of try. More details here and here.
Was This Post Helpful? 0
  • +
  • -

#4 Bacanze  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 202
  • Joined: 09-April 10

Re: Try Catch Block

Posted 25 July 2010 - 09:28 AM

Things within a try catch block are counted as a separate scope as mentioned above, therefore things declared cannot be seen, here's a poor example.

 private void CooMethod()
        {
            int i = 10;

            try
            {
                int result = i + 1;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.WriteLine(result); //can't see result variable, won't compile

        }

Was This Post Helpful? 0
  • +
  • -

#5 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Try Catch Block

Posted 25 July 2010 - 10:12 AM

In other words the try block is not making things private, what makes the client private is that it is being declared inside the try block. So why dont you try it to declare it outside the try block.
Was This Post Helpful? 0
  • +
  • -

#6 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Try Catch Block

Posted 25 July 2010 - 09:11 PM

Here's an easy example which will hopefully help to explain scope to you.

        int[] anArray = { 1, 2, 3 };
        int value;
        int index = 3;

        try
        {
            value = anArray[index];
        }
        catch (IndexOutOfRangeException e)
        {
            Console.WriteLine(e);//don't need to use ToString() method here
        }

        value = 3;



In the above example the variables anArray, value, and index can be seen by everything in this program. They are not declared within any blocks({...}). So you can use value inside the try, and again outside of it after the catch.

Now look at this similar example:

        int[] anArray = { 1, 2, 3 };
        int index = 3;

        try
        {
            int value = anArray[index];
        }
        catch (IndexOutOfRangeException e)
        {
            Console.WriteLine(e);//don't need to use ToString() method here
        }

        value = 3;



Now this example is erroneous. The line "value = 3;" will cause an error. Because value is declared inside the try block, it cannot be used elsewhere because it does not exist elsewhere. It is local to that scope.

Hope this helps!

This post has been edited by eZACKe: 25 July 2010 - 09:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1