16

Quote from MSDN:

If Finalize or an override of Finalize throws an exception, the runtime ignores the exception, terminates that Finalize method, and continues the finalization process.

Yet if I have:

~Person()
{
throw new Exception("meh");
}

then it results in a runtime exception?

p.s. I know that this should never happen, however I'm just curious around this behaviour. One of our clients had an empty try catch around all of their finalizers.. it didn't even log when things went wrong or reserect the object :/

6
  • 2
    You should never use finalizers anyway. Never as in, unless you know exactly why you have to use that.
    – Dykam
    Apr 22, 2010 at 15:10
  • "then it results in a runtime exception?" Are you saying you've tried this and it does result in an exception? Or are you asking us to test this?
    – AaronLS
    Apr 22, 2010 at 15:11
  • 1
    it DOES result in a runtime exception. Dykam, thanks for telling me I should never use finalizers... lol!
    – sjhuk
    Apr 22, 2010 at 15:15
  • Finalizers are for releasing unmanaged resources. Managed resources are in an indeterminant state while the Finalizer is running. If you're throwing an exception in a finalizer, then you should change your design pattern to prevent the exception from occurring (i.e. check the file exists before accessing, etc)
    – ajs410
    Apr 22, 2010 at 15:20
  • 2
    Read my post! "I know that this should never happen" This isn't a specific question, just a generalization since this was mentioned in the O'Reilly .NET 4.0 book AND on some old MSDN documentation.
    – sjhuk
    Apr 22, 2010 at 15:25

2 Answers 2

26

Linking the source of your quote is important. I have to assume it talks about an old version of .NET, perhaps version 1.x. It tried to be "tolerant" of unhandled exceptions, swallowing them without a squeak. That did not work out well, chunks of code silently failing is extraordinarily hard to debug.

The .NET 2.0 version put an end to that, the default CLR host terminates the app for any unhandled exception. An exception in a finalizer is fatal.

2
0

I'm curious as to what happens in xamarin since i've seen this happen in production and the android app did not crash, it is possible that a lock occurred on the finalizer thread and the app ran sub-par until restart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.