Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 686 Vote(s) - 3.38 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why use try {} finally {} with an empty try block?

#1
I noticed in `System.Threading.TimerBase.Dispose()` the method has a `try{} finally{}` block but the `try{}` is empty.

Is there any value in using `try{} finally{}` with an empty `try`?

[To see links please register here]


[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
internal bool Dispose(WaitHandle notifyObject)
{
bool status = false;
bool bLockTaken = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
}
finally {
do {
if (Interlocked.CompareExchange(ref m_lock, 1, 0) == 0) {
bLockTaken = true;
try {
status = DeleteTimerNative(notifyObject.SafeWaitHandle);
}
finally {
m_lock = 0;
}
}
Thread.SpinWait(1);
// yield to processor
}
while (!bLockTaken);
GC.SuppressFinalize(this);
}

return status;
}
Reply

#2
This is to guard against `Thread.Abort` interrupting a process. [Documentation][1] for this method says that:

> Unexecuted finally blocks are executed before the thread is aborted.

This is because in order to recover successfully from an error, your code will need to clean up after itself. Since C# doesn't have C++-style destructors, `finally` and `using` blocks are the only reliable way of ensuring that such cleanup is performed reliably. Remember that `using` block turns into this by the compiler:

try {
...
}
finally {
if(obj != null)
((IDisposable)obj).Dispose();
}

In .NET 1.x, there was a chance that `finally` block will get aborted. This behavior was changed in .NET 2.0.

Moreover, empty `try` blocks never get optimized away by the compiler.


[1]:

[To see links please register here]

Reply

#3
From [

[To see links please register here]

][1]:

> This methodology guards against a
> Thread.Abort call interrupting the
> processing. The MSDN page of
> Thread.Abort says that “Unexecuted
> finally blocks are executed before the
> thread is aborted”. So in order to
> guarantee that your processing
> finishes even if your thread is
> aborted in the middle by someone
> calling Abort on your thread, you can
> place all your code in the finally
> block (the alternative is to write
> code in the “catch” block to determine
> where you were before “try” was
> interrupted by Abort and proceed from
> there if you want to).


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through