|
-
Apr 18th, 2003, 04:34 PM
#1
Thread Starter
yay gay
Jumping to a finally {}
i have a following try catch block
try {
if () {}
else { NOW i wanna go directly to a finally{} block
}
is there a way to do that?
\m/  \m/
-
Apr 18th, 2003, 07:57 PM
#2
Frenzied Member
Well you can use goto, but its frowned upon.
Dont gain the world and lose your soul
-
Apr 18th, 2003, 08:03 PM
#3
Thread Starter
yay gay
but how? goto finally
? or what?
\m/  \m/
-
Apr 18th, 2003, 08:38 PM
#4
PowerPoster
I haven't tried it, but try the break statement:
break;
It is useful in loops when you want out, maybe you can break out of a try block...
-
Apr 18th, 2003, 08:43 PM
#5
Thread Starter
yay gay
no, i dont think so because its just for loops and switches
\m/  \m/
-
Apr 18th, 2003, 08:44 PM
#6
PowerPoster
Nevermind, just tried it, it only works for loops.
-
Apr 18th, 2003, 08:47 PM
#7
Thread Starter
yay gay
i cant make goto to a finally block?
\m/  \m/
-
Apr 18th, 2003, 09:38 PM
#8
PowerPoster
After thinking about what your trying to do, I think you are probably approaching the problem wrong. You shouldn't be using error trapping as flow control for your program. You should use it to trap and handle errors. Could you maybe post the whole code your trying to do so I can see if there is a different way for you to go about it.
Jumping to different blocks of code in the same method isn't good practice anyway.
-
Apr 18th, 2003, 09:40 PM
#9
PowerPoster
Looking at your example, you can just leave out the else part of the if statement and if the statement isn't true, the finally block will execute:
Code:
try
{
if()
{
// your code here
}
}
catch
{
}
finally
{
//Your finally code here.
}
-
Apr 18th, 2003, 11:48 PM
#10
Thread Starter
yay gay
i had the idea of throwing my own exception
\m/  \m/
-
Apr 19th, 2003, 08:23 AM
#11
No, that's bad.
But your problem is irrelevant IMO. It seems you want to do something like
Code:
try {
// stuff
if(expr) {
// stuff
} else {
// stuff
}
// stuff you don't want if it's else
} finally {
// stuff
}
In that case it should be
Code:
try {
// stuff
if(expr) {
// stuff
// stuff you don't want if it's else
} else {
// stuff
}
} finally {
// stuff
}
The finally blocks executes even if there is no error.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 19th, 2003, 04:51 PM
#12
PowerPoster
Use the return keyword to exit the routine. B/c you are using a finally statement, it will always fire:
Code:
private void button1_Click(object sender, System.EventArgs e)
{
bool flag = false;
try
{
if (flag)
{
Console.WriteLine("Do Something..");
}
else
{
return;
}
}
finally
{
MessageBox.Show("Finally Block Executing..");
}
}
-
Apr 19th, 2003, 04:54 PM
#13
Thread Starter
yay gay
always? didnt know of that..iamgine the following example:
Code:
try {
if (a == 5) {
return true;
else {
MessageBox.show("error");
}
}
finally {
return false;
}
}
this means that it will return false if the value is equal to 5?
\m/  \m/
-
Apr 19th, 2003, 04:57 PM
#14
PowerPoster
You can't use the return keyword in the finally() clause.
-
Apr 19th, 2003, 04:58 PM
#15
Thread Starter
yay gay
\m/  \m/
-
Apr 20th, 2003, 03:44 PM
#16
But if you were using the Windows SEH in C or C++ then it would always return false, no matter what the value of a is.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|