ddayan
ddayan

Reputation: 4142

Handling Ruby "failed with.... "

def slicer_imlib2(zoom_level, file_path, output_path)
    begin
      tile_size = 256
      file_path = "public/#{file_path}"

      image = Imlib2::Image.load_image file_path

      image_width = image.width
      image_height = image.height
.....
....
    end
      end
      image = nil
    rescue
      return false
    else
      return true
    end
end

The begin rescue blocks don't work

I'm getting

[Worker()] UserImage#slice_lib failed with Imlib2::Error::DeletedError: image deleted - 0 failed attempts
[Worker()] UserImage#slice_lib failed with Imlib2::Error::DeletedError: image deleted - 0 failed attempts
[Worker()] UserImage#slice_lib failed with Imlib2::Error::DeletedError: image deleted - 0 failed attempts
[Worker()] 3 jobs processed at 32.5508 j/s, 3 failed ...

I'm trying to return false if im getting Imlib2::Error::DeletedError: image deleted

Upvotes: 0

Views: 89

Answers (2)

Spyros
Spyros

Reputation: 48626

J-_-L gives you a good answer, but i just wanted to say something on the practices side that may save you from some hassle.

In your example, i would not use exceptions. If you want to check whether an image is deleted or not, it's better to use a standard find and handle the return value.

An exception should generally be used NOT to handle user input but look for weird things happening. For instance, a method that expects an argument to be either 1 or 0, suddenly gets 2 as input. That would be a proper exception handling place.

Upvotes: 0

J-_-L
J-_-L

Reputation: 9177

Try

begin
  # ...
rescue Imlib2::Error::DeletedError
  # ...
else
  # ...
end

rescue (without specifing an ErrorClass) does only catch StandardErrors (Imlib2::Error::DeletedError directly inherits from Exception).

Upvotes: 1

Related Questions