Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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 :)

Reply With Quote
  #2 (permalink)  
Old January 12th, 2007, 07:11 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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?
Reply With Quote
  #3 (permalink)  
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:08 AM.


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