user2483172
user2483172

Reputation: 11

How do I raise an Exception when execution exceeds 60 seconds?

I am evaluating some code and, at times, see that the Ruby code I am executing runs over a 60 second mark.

Does anyone have an example of raising an exception based on a time limit?

Upvotes: 1

Views: 193

Answers (1)

usha
usha

Reputation: 29349

require "timeout"
Timeout.timeout(60) do
   <your code here>
end

This will raise a Timeout::Error exception. You can also pass in your custom exception to the timeout:

Timeout.timeout(60, <CustomException>)

Refer to the Timeout documentation for more information.

Upvotes: 5

Related Questions