0 votes
in Ruby by
Explain the use of ensure statement in Ruby?

1 Answer

0 votes
by

There is an ensure clause which guarantees some processing at the end of code. The ensure block always run whether an exception is raised or not. It is placed after last rescue clause and will always executed as the block terminates.

The ensure block will run at any case whether an exception arises, exception is rescued or code is terminated by uncaught exception.

Syntax:

begin  

code..  

#..raise exception  

rescue  

#.. exception is rescued  

ensure  

#.. This code will always execute.  

end  

...