Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 12th, 2007, 05:42 PM
Registered User
 
Join Date: Dec 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Try/Catch/Finally

I have a query that is related to try/catch/finally. I m giving an example:
myfunction()
{
try
{
System.IO.StreamReader objReader = new System.IO.StreamReader("text.txt");
Console.WriteLine(objReader.ReadToEnd());
}
catch(System.IO.FileNotFoundException exc)
{
new Exception(exc.Message);
}
finally
{
objReader.Close();
}
}

Now this code would not compile as the objReader is declared within the try block and the variable that you declared in try block can't be accessed in finally block. Now my question is Why is it so? i want to know the internal implementation of try/catch/finally. Thanks in advance :)

 
Old January 12th, 2007, 07:11 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Just declare the StreamReader *outside* the try block and instantiate it *inside* the try block....

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: Butterflies & Hurricanes by Muse (Track 10 from the album: Absolution) What's This?
 
Old January 12th, 2007, 11:44 PM
Friend of Wrox
 
Join Date: May 2006
Location: San Diego, CA, USA.
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by rohit.sharma
 I have a query that is related to try/catch/finally. I m giving an example:
myfunction()
{
try
{
System.IO.StreamReader objReader = new System.IO.StreamReader("text.txt");
Console.WriteLine(objReader.ReadToEnd());
}
catch(System.IO.FileNotFoundException exc)
{
new Exception(exc.Message);
}
finally
{
objReader.Close();
}
}

Now this code would not compile as the objReader is declared within the try block and the variable that you declared in try block can't be accessed in finally block. Now my question is Why is it so? i want to know the internal implementation of try/catch/finally. Thanks in advance :)
This is a scope issue that is not specific to try/catch/finally. When you declare a variable within a particular block, it is visible only within that block. A block is defined with opening and closing braces. In the following case, intCounter only can be used in the "if block" it is declared in, and will only be created only if someCondition is true:
Code:
if (someCondition)
{
   int counter ;
   for (counter=1; counter<=100; counter++)
   // Do important things here
}
You can also declare blocks simply by enclosing code withing braces as you please. There usually isn't a pressing reason to do this, but it can be useful occasionally:

Code:
{
   int myValue = 10;
   Console.WriteLine(myValue);
}
Woody Z
http://www.learntoprogramnow.com




Similar Threads
Thread Thread Starter Forum Replies Last Post
Try...Catch lowell VB.NET 3 July 23rd, 2007 06:35 AM
Re: Try/Finally to prevent connection pooling leak spinout ASP.NET 2.0 Basics 4 June 19th, 2007 08:48 AM
Try Catch not working?? dparsons ASP.NET 1.0 and 1.1 Professional 3 September 18th, 2006 06:54 PM
Try and Catch? mujju PHP How-To 2 January 20th, 2005 12:27 PM
variable initialization with try..finally guppyheart C# 7 July 17th, 2003 06:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.