Should OnExit be contained within a finally block?
See original GitHub issueShould
public class Sample {
public void Method(int value) {
InterceptorAttribute attribute =
(InterceptorAttribute) Activator.CreateInstance(typeof(InterceptorAttribute));
// in c# __methodref and __typeref don't exist, but you can create such IL
MethodBase method = MethodBase.GetMethodFromHandle(__methodref (Sample.Method),
__typeref (Sample));
object[] args = new object[1] { (object) value };
attribute.Init((object)this, method, args);
attribute.OnEntry();
try {
Debug.WriteLine("Your Code");
attribute.OnExit();
}
catch (Exception exception) {
attribute.OnException(exception);
throw;
}
}
}
be
public class Sample {
public void Method(int value) {
InterceptorAttribute attribute =
(InterceptorAttribute) Activator.CreateInstance(typeof(InterceptorAttribute));
// in c# __methodref and __typeref don't exist, but you can create such IL
MethodBase method = MethodBase.GetMethodFromHandle(__methodref (Sample.Method),
__typeref (Sample));
object[] args = new object[1] { (object) value };
attribute.Init((object)this, method, args);
attribute.OnEntry();
try {
Debug.WriteLine("Your Code");
}
catch (Exception exception) {
attribute.OnException(exception);
throw;
}
finally {
attribute.OnExit();
}
}
}
Issue Analytics
- State:
- Created 8 years ago
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Nested try-finally in C# - Stack Overflow
The finally block is useful for cleaning up any resources that are allocated in the try block, and for running any code that...
Read more >coding style - Throwing an exception inside finally
Basically, finally clauses are there to ensure proper release of a resource. However, if an exception is thrown inside the finally block, ...
Read more >The finally Block - Essential Java Classes
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception...
Read more >Error-Handling Operations
The on-error and on-exception blocks contain the code to handle errors that occur in the monitor block. A monitor block consists of a...
Read more >How to Implement Try Catch Finally Blocks in PHP
The finally block should contain code that is always executed, regardless of whether an exception is thrown or not. Try Catch Finally Syntax....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@alexeysuvorov I think that adding the OnFinally as suggested by @ProESM to preserve backwards compatability would be a smarter way to go.
We could make decorators to handle lock statements (through
Monitor
) if we had anOnFinally
method available, I think it would be very useful.