473,721 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about scope of try variables

Hi all,
The variables declared inside try block cant be accessed inside its
corresponding finally block. When I used to explain finally concept to anyone
I would usually tell them (briefly) that the finally block is existing there
cuz of the logic inside try block, if try is not there finally wont be there.
If try and finally are so closely related than finally should share the scope
of try. Please explain the rational behind this scope restriction.

I wanted to do
try
{
string tmp = this.Text;
...
}
catch{}

finally
{

this.Text = tmp;
}

but since I cant access try scope variable in finally sshould I move the
settings outside them like:

string tmp = this.Text
try ..
finaly {}
this.Text = tmp;

Thanks & Regards,

Abubakar.
http://joehacker.blogspot.com

Nov 17 '05 #1
2 2038

"Abubakar" <Ab******@discu ssions.microsof t.com> wrote in message
news:5C******** *************** ***********@mic rosoft.com...
Hi all,
The variables declared inside try block cant be accessed inside its
corresponding finally block. When I used to explain finally concept to
anyone
I would usually tell them (briefly) that the finally block is existing
there
cuz of the logic inside try block, if try is not there finally wont be
there.
If try and finally are so closely related than finally should share the
scope
of try. Please explain the rational behind this scope restriction.


Finally has a specific purpose, that is the code within that block will be
executed if an exception occurs in the try block or not. In other words it
is almost always executed(it is possible that certain big failures could
cause finally blocks not to execute, but these are rare and generally result
in dead programs anyway.) Because the purpose of a finally block is to run
code whether or not the stuff inside the try block actually executed, it
doesn't make sense to be able to use the variables within the try block,
since the assignment might not have happened(code before the assignment may
have failed, the initalizer in the assignment might have failed, or a random
error could have occured between initalization(r eading from this.Text) and
the actual assignment to tmp).

To do what you want:

string tmp = this.Text;
try
{

}
finally
{
this.Text = tmp;
}


Nov 17 '05 #2
I like this one :
since the assignment might not have happened(code before the assignment may
have failed, the initalizer in the assignment might have failed, or a random
makes a lot sense

Thanks.

Abubakar.
http://joehacker.blogspot.com

"Daniel O'Connell [C# MVP]" wrote:

"Abubakar" <Ab******@discu ssions.microsof t.com> wrote in message
news:5C******** *************** ***********@mic rosoft.com...
Hi all,
The variables declared inside try block cant be accessed inside its
corresponding finally block. When I used to explain finally concept to
anyone
I would usually tell them (briefly) that the finally block is existing
there
cuz of the logic inside try block, if try is not there finally wont be
there.
If try and finally are so closely related than finally should share the
scope
of try. Please explain the rational behind this scope restriction.


Finally has a specific purpose, that is the code within that block will be
executed if an exception occurs in the try block or not. In other words it
is almost always executed(it is possible that certain big failures could
cause finally blocks not to execute, but these are rare and generally result
in dead programs anyway.) Because the purpose of a finally block is to run
code whether or not the stuff inside the try block actually executed, it
doesn't make sense to be able to use the variables within the try block,
since the assignment might not have happened(code before the assignment may
have failed, the initalizer in the assignment might have failed, or a random
error could have occured between initalization(r eading from this.Text) and
the actual assignment to tmp).

To do what you want:

string tmp = this.Text;
try
{

}
finally
{
this.Text = tmp;
}


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2228
by: William Gill | last post by:
Working with tkinter, I have a createWidgets() method in a class. Within createWidgets() I create several StringVars() and assign them to the textvariable option of several widgets. Effectively my code structure is: def createWidgets(self): ... var = StringVar() Entry(master,textvariable=var) ...
9
2210
by: Stefan Turalski \(stic\) | last post by:
Hi, I done sth like this: for(int i=0; i<10; i++) {...} and after this local declaration of i variable I try to inicialize int i=0;
10
1884
by: John Salerno | last post by:
Here's a sentence from Learning Python: "Names not assigned a value in the function definition are assumed to be enclosing scope locals (in an enclosing def), globals (in the enclosing module's namespace) or built-in (in the predefined __builtin__ names module Python provides." I have trouble reading this sentence. First, I don't understand if the word 'enclosing' is a verb or an adjective. The whole flow of the sentence seems...
7
1875
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that shows the alert. What is the essential reason? function () { alert('hi mom'); }(); function () { alert('hi dad'); }(8); var x=function () { alert('hi bro'); }();
7
2114
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9 ----------------------------------------------------------------------------- This is a consortium of ideas from another thread on topic ----------------------------------------------------------------------------- One of the big issues of organizing items within a class, is there are many...
7
5159
by: int main(void) | last post by:
Hi all, I know that register variables work fine at block scope. I tried putting a register variable in file scope, like this, #include <stdio.h> register int a = 2; int main(void) { printf("%d\n",a);
1
25690
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
0
35243
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For the sake of brevity I am sticking to common usage. Wherever the term procedure is used in this tutorial it actually refers to a subroutine or function. Definition of Scope The scope of a variable where this variable can be seen or accessed...
6
1628
by: lazy | last post by:
hi, I have some constants defined in a php script say config.php. I want to use the variables there defined in other scripts. couple of questions regd that: 1. Is there an alternative to including config.php and declaring the variables that will be used as global. This seems very inefficient. 2.Moreover these variables are constants, is there a way to make the variables unmodifiable in config.php so that scripts that use them ,
12
3013
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables. Only very few global functions are allowed to be reusability for the programmers to use. Few...
0
8727
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9365
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8002
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4483
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4748
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3186
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2127
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.