question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Should OnExit be contained within a finally block?

See original GitHub issue

Should

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:closed
  • Created 8 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Tybieleecommented, Apr 18, 2016

@alexeysuvorov I think that adding the OnFinally as suggested by @ProESM to preserve backwards compatability would be a smarter way to go.

0reactions
Logerfocommented, Jan 22, 2019

We could make decorators to handle lock statements (through Monitor) if we had an OnFinally method available, I think it would be very useful.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found