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?

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

3 Comments

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).
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.
Just for information: it is better to use "trap" for handling UNIX signals.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.