Is it possible to use return statement in try block?.How,What is the use of the statement.
Asked
Active
Viewed 1,055 times
-3
-
15-1 for not bothering to try – Matti Virkkunen May 13 '10 at 16:15
-
i had problem in my system...so i cant now. – Ayyappan Anbalagan May 13 '10 at 16:17
-
2You've already been given the correct answer below. I just want to point out that even if you do have a return statement in a try block, any finally block will still execute. – Jay Riggs May 13 '10 at 16:17
-
4@Ayyappan.Anbalagan: If you have "a problem in your system" that's preventing you from running any code, why do you need to know this right now? – Matti Virkkunen May 13 '10 at 16:18
-
1@Matti Virkkunen, They are parallelizing the "fix computer" and "learn C#" event loops. – strager May 13 '10 at 16:20
-
Mr.Matti Virkkunen.. Today, i got this question in my technical round interview... – Ayyappan Anbalagan May 13 '10 at 16:25
-
@Ayyappan.Anbalagan: What computer are you using to post anyways? Did the "problem in your system" mysteriously disappear? – Matti Virkkunen May 13 '10 at 16:31
-
1+1 While you could have tried it out, this is an interesting question. – helpermethod May 13 '10 at 17:10
-
To all the downvoters, "trying it out" is not the most reliable technique, it is unlikely to reveal all pre- and post-conditions. This might be a duplicate but otherwise it's a good and valid question. – H H May 13 '10 at 19:44
-
1@Henk Holterman: In that case it would've been better to ask "what are the possible pitfalls" instead of just asking "can I do it" – Matti Virkkunen May 13 '10 at 19:55
5 Answers
8
You can return from within a try block, but keep in mind that the code in the finally clause will be executed before returning from the method. For example, calling MessageBox.Show(test().ToString());
using the method below will cause two message boxes to appear (the first displaying "3" and the second displaying "1").
int test() { try { return 1; throw new Exception(); } catch (Exception e) { return 2; } finally { MessageBox.Show("3"); } }

TreDubZedd
- 2,571
- 1
- 15
- 20
-
4Note also that attempting to return from the finally clause results in a compiler error. – TreDubZedd May 13 '10 at 17:06
-
+1 to both the answer and the comment for learning me something about the finally block today. – Chuck Wilbur May 13 '10 at 21:45
4
Sure, you can use return in a try block, and the syntax is just like everywhere else.
try
{
return 0;
}
catch (Exception e)
{
// handle exception
}

dcp
- 54,410
- 22
- 144
- 164
0
Yes.
I can use local variables without having to widen their scope, for example.
int Foo(string bar) {
try {
int biz = int.Parse(bar);
return biz;
} catch(...) {
// Handle bad `bar`
}
}

strager
- 88,763
- 26
- 134
- 176
0
An answer for this question is Yes. As an example for this you can refer to the following question: finally and return Hope you also get a clarification.
-1
Sure, you can do it like:
public string X(..)
{
try
{
//do something
return value;
}
catch(Exception ex)
{
throw;
//or return here
}
}

Brian Mains
- 50,520
- 35
- 148
- 257
-
It's important that you either throw or return in the catch block. If the try code throws an exception, the return in the try block will not be executed. – Instance Hunter May 13 '10 at 16:17