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  

Related questions

0 votes
asked Nov 11, 2021 in Ruby by SakshiSharma
0 votes
0 votes
asked Nov 14, 2021 in Ruby by sharadyadav1986
...