Is there any way to throw an exception in a finalizer?

F

Francis Hwang

I'm trying to do write test code that involves a mock object throwing
an exception on object destruction if a certain condition isn't met.
(In particular, I want to guarantee that a class that uses DBI calls
@dbh.disconnect.) I've been trying to set a finalizer with
ObjectSpace.define_finalizer, but it seems like the GC doesn't want me
to raise an exception from within a finalizer. Overall I understand --
I probably wouldn't try anything this gnarly in production code -- but
is there any way for me to get around this for test code?

Thanks in advance,

Francis
 
Y

Yukihiro Matsumoto

Hi,

In message "Is there any way to throw an exception in a finalizer?"

|I'm trying to do write test code that involves a mock object throwing
|an exception on object destruction if a certain condition isn't met.
|(In particular, I want to guarantee that a class that uses DBI calls
|@dbh.disconnect.) I've been trying to set a finalizer with
|ObjectSpace.define_finalizer, but it seems like the GC doesn't want me
|to raise an exception from within a finalizer. Overall I understand --
|I probably wouldn't try anything this gnarly in production code -- but
|is there any way for me to get around this for test code?

You cannot assume when and where finalizers are called, and they are
not guaranteed to be called. I don't think it's a good idea to raise
exception from within finalizers.

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top