Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#: How to handle finalizer exceptions from a 3rd-party library?

Finalizers are always called by .net framework, so the sequence could be out of control; and even if the constructor failed, the destructor still can be triggered.

This could bring troubles, when such finalizer exceptions come from a 3rd-party library: I can't find a way to handle them!

For example, in the code below, although class A's constructor always throw an exception and fail, finalizer of A will be triggered by the .net framework, also ~B() is called as A has a property of B type.

class Program // my code
{
    static void Main(string[] args)
    {
        A objA;
        try
        {
            objA = new A();
        }
        catch (Exception)
        {
        }

        ; // when A() throws an exception, objA is null

        GC.Collect(); // however, this can force ~A() and ~B() to be called.

        Console.ReadLine();
    }
}

public class A  // 3rd-party code
{
    public B objB;

    public A()
    {
        objB = new B(); // this will lead ~B() to be called.
        throw new Exception("Exception in A()");
    }

    ~A() // called by .net framework
    {
        throw new Exception("Exception in ~A()"); // bad coding but I can't modify
    } 
}

public class B // 3rd-party code
{
    public B() { }

    ~B() // called by .net framework
    {
        throw new Exception("Exception in ~B()"); // bad coding but I can't modify
    } 
}

If these are my code, it is a bit easier - I can use try-catch in finalizers, at least I can do some logging - I can allow the exception to crash the program, to discover the error asap - or if I want to "tolerate" the exception, I can have a try-catch to suppress the exception, and have a graceful exit.

But if A and B are classes from a 3rd-party library, I can do nothing! I can't control the exception to happen, I can't catch them, so I can't log it or suppress it!

What can I do?

like image 497
athos Avatar asked Jul 18 '11 01:07

athos


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

Sounds like the 3rd party utility is poorly written. :)

Have you tried catching it using AppDomain.UnhandledException?

like image 112
CodingWithSpike Avatar answered Oct 28 '22 13:10

CodingWithSpike