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

  • Thread starter Thread starter Polaris431
  • Start date Start date
P

Polaris431

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?
 
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
 
Jon Skeet said:
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
 
Polaris431 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top