473,408 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Is it possible to inspect the return value in the 'finally' part of a try...catch without using a variable?

If I have a method like this:

private void DoSomething()
{
try
{
return true;
}
catch{}
finally
{
// Can I inspect the return value here without using a variable?
}
}

Is there something in C# that would allow me to observe the return
value in the 'finally' section without having to store the return
value in a variable in the 'try' section?

Sep 27 '07 #1
4 1313
On Sep 27, 9:57 am, Polaris431 <johannbl...@gmail.comwrote:
If I have a method like this:

private void DoSomething()
{
try
{
return true;
}
catch{}
finally
{
// Can I inspect the return value here without using a variable?
}

}

Is there something in C# that would allow me to observe the return
value in the 'finally' section without having to store the return
value in a variable in the 'try' section?
No. Don't forget that you may not *have* a return value, depending on
how you reached the finally block.

Jon

Sep 27 '07 #2
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@w3g2000hsg.googlegro ups.com...
On Sep 27, 9:57 am, Polaris431 <johannbl...@gmail.comwrote:
>If I have a method like this:

private void DoSomething()
{
try
{
return true;
}
catch{}
finally
{
// Can I inspect the return value here without using a variable?
}

}

Is there something in C# that would allow me to observe the return
value in the 'finally' section without having to store the return
value in a variable in the 'try' section?

No. Don't forget that you may not *have* a return value, depending on
how you reached the finally block.

Jon
Especially on a void method.
The example doesn't even compile.

- Michael Starberg
Sep 27 '07 #3
No. you have to define a varible.

"Polaris431" <jo*********@gmail.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
If I have a method like this:

private void DoSomething()
{
try
{
return true;
}
catch{}
finally
{
// Can I inspect the return value here without using a variable?
}
}

Is there something in C# that would allow me to observe the return
value in the 'finally' section without having to store the return
value in a variable in the 'try' section?
Sep 28 '07 #4
Polaris431 wrote:
If I have a method like this:

private void DoSomething()
{
try
{
return true;
}
catch{}
finally
{
// Can I inspect the return value here without using a variable?
}
}

Is there something in C# that would allow me to observe the return
value in the 'finally' section without having to store the return
value in a variable in the 'try' section?
As other have stated: the short answer is no.

And in most cases wanting to do so indicate that somehow
the code has been less than optimal designed.

If you need to add some "post processing" that are
really not related to the code itself, then you could
look at AOP techniques and use an around advice.

Arne
Oct 3 '07 #5

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

Similar topics

0
by: Dave | last post by:
Hi everyone, (I already posted this to the VS.NET IDE news group without any responses, so I'm attempting one more time in this group) The issue I'm having is occuring in the IDE of VS.NET...
0
by: Andrew Dowding | last post by:
Hi Everybody, I have been looking at problems with my Windows Forms C# application and it's little Jet 4 (Access) database for the last few days. The Windows Forms app implements a facade and...
4
by: Wal Turner | last post by:
Consider the following simple class: public class ClassA { public static string VAR = "hello"; } public void MethodA() { while(true)
24
by: trint | last post by:
add them into one PrintDocument: PrintDocument pd1 = new PrintDocument(); PrintDocument pd2 = new PrintDocument(); PrintDocument pdCombined = new PrintDocument(); pdCombined = pd1 + pd2;...
5
by: Brian Kitt | last post by:
I have a C# application that builds dynamic HTML and renders it. Because it is rendered in this way, the input controls are not server controls. I write the entire page, which has a variable...
13
by: Alison Givens | last post by:
....... that nobody knows the answer. I can't imagine that I am the only one that uses parameters in CR. So, my question again: I have the following problem. (VB.NET 2003 with CR) I have a...
20
by: alainpoint | last post by:
Hi I wonder if Python is capable of the following: define a function which returns its argument. I mean: def magic_function(arg): ...... some magic code ... that behaves the following way:
4
by: elventear | last post by:
Hello, I am trying to use someone else's module that makes use of inspect.getsourcelines. The code was not working for me, so I have been debugging to see why it is not working. I have reduced...
9
by: igor.tatarinov | last post by:
Hi, I am pretty new to Python and trying to use it for a relatively simple problem of loading a 5 million line text file and converting it into a few binary files. The text file has a fixed format...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.