4

I am newbie in Ruby, coming from Java world.

I just want to copy a file in Ruby: http://apidock.com/ruby/FileUtils/cp

However, the docs doesn't tell what exceptions will be raised. Compare to Javadocs:

http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)

Without looking at FileUtils' source code, is there any way to tell which exceptions would potentially be raised?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Pahlevi Fikri Auliya
  • 4,157
  • 8
  • 35
  • 71

1 Answers1

6

There is no guarantee of which exception will be raised in Ruby. A user could send an interrupt, your file system may not be available to write to, etc. And Ruby makes no guarantees for this. But for File operations, I would likely look at IOError and its child, EOFError to start.

This may give the ability to handle the non-exceptional (reasonably expected) events, such as a file not existing, or not having read access or not having write access, these things you can program for, and rescue and attempt to handle.

You can also write your own Exceptions, raise your custom Exceptions (probably inheriting StandardError) and give the appropriate action or feedback.

This is the current Exception Hierarchy generated just now from my computer using Ruby 1.9.3-p327

BasicObject
  Exception
    NoMemoryError
    ScriptError
      LoadError
        Gem::LoadError
      NotImplementedError
      SyntaxError
    SecurityError
    SignalException
      Interrupt
    StandardError
      ArgumentError
      EncodingError
        Encoding::CompatibilityError
        Encoding::ConverterNotFoundError
        Encoding::InvalidByteSequenceError
        Encoding::UndefinedConversionError
      FiberError
      IOError
        EOFError
      IndexError
        KeyError
        StopIteration
      LocalJumpError
      Math::DomainError
      NameError
        NoMethodError
      RangeError
        FloatDomainError
      RegexpError
      RuntimeError
        Gem::Exception
          Gem::CommandLineError
          Gem::DependencyError
          Gem::DependencyRemovalException
          Gem::DocumentError
          Gem::EndOfYAMLException
          Gem::FilePermissionError
          Gem::FormatException
          Gem::GemNotFoundException
          Gem::GemNotInHomeException
          Gem::InstallError
          Gem::InvalidSpecificationException
          Gem::OperationNotSupportedError
          Gem::RemoteError
          Gem::RemoteInstallationCancelled
          Gem::RemoteInstallationSkipped
          Gem::RemoteSourceException
          Gem::VerificationError
      SystemCallError
      ThreadError
      TypeError
      ZeroDivisionError
    SystemExit
      Gem::SystemExitException
    SystemStackError
    fatal
vgoff
  • 10,980
  • 3
  • 38
  • 56
  • In general, what is the best way to handle exception in Ruby? Since the documentation never mention anything about possible exceptions raised, how could we catch fine grained Exception? => If we catch the root object, i.e. Exception, I think it would be too general (may handle something that the method shouldn't handle). – Pahlevi Fikri Auliya Dec 17 '12 at 01:49
  • This is probably a pretty good new question, actually. It is general enough to be helpful, and it perhaps doesn't clarify the current question. But in short, let's say that you are wanting to catch a user interrupt. We might rescue `SignalException` but that would rescue other exceptions that we may not want to rescue. So then we look at the child exception shown in the table, which is to say `Interrupt`. Now we can gracefully close a program where you might have someone press `ctrl-c`. – vgoff Dec 17 '12 at 05:42
  • 1
    Just for information: it is better to use "trap" for handling UNIX signals. – Simon Perepelitsa May 18 '13 at 11:36