-2

i manage to do "something"(like deleting files,etc) when exit or exit! is called from Object. by changing the method exit and exit! inside Object class. (at_exit is too unreliable)

but then the "something" never execute if there's error such as NameError, etc.

is there way so i can make "something" that still execute if there's error. (any error possible if necessary).

something like at_exit but works with all errors

thanks in advance for assistance. and forgive me if there's already question ask for this. i do search a lot before asking here. and found nothing.

edit: i don't know where the original author of the code place the method. since the original author load it from dll files in the exe program the author used for launcher. (i can only edit After the original code take place...). so i think i need another approach for this... but i manage to make a workaround for my problem... by putting begin rescue in other places that send data to the original object. i filter the data send and throw my own error before it reach the main program... so i guess this works too.

Andi Msp
  • 1
  • 2

1 Answers1

1

Borrowing from an answer on a different thread, and definitely along the lines of what Marek commented, this is how you should handle errors in Ruby:

begin
  # something which might raise an exception
rescue SomeExceptionClass => some_variable
  # code that deals with some exception
rescue SomeOtherException => some_other_variable
  # code that deals with some other exception
else
  # code that runs only if *no* exception was raised
ensure
  # ensure that this code always runs, no matter what
end

Original credit: Begin, Rescue and Ensure in Ruby?

Community
  • 1
  • 1
tomtom
  • 1,130
  • 2
  • 11
  • 35