Handling File.open exceptions

C

Carl Jenkins

I was reading the pick axe book (for 1.9 version of ruby) and on page
155 it talks about exception handling.

More specifically, it says that beginners usually make the mistake of
putting File.open in the begin end block.

Now, I am a ruby newbie but, how do you handle exceptions for File.open
if it is not in the begin block? Do I put File.open in its own begin
block?
 
R

Robert Klemme

I was reading the pick axe book (for 1.9 version of ruby) and on page
155 it talks about exception handling.

More specifically, it says that beginners usually make the mistake of
putting File.open in the begin end block.

Now, I am a ruby newbie but, how do you handle exceptions for File.open
if it is not in the begin block? Do I put File.open in its own begin
block?

Does this help?

http://blog.rubybestpractices.com/posts/rklemme/001-Using_blocks_for_Robustness.html

Kind regards

robert
 
R

Robert Klemme

Yes - thanks that does help.
Good.

But, I have to be honest it seems a bit strange to not have to use
exception handling. Especially, when coming from the Java way of doing
things.

You still need to handle the exception somewhere unless you want to let
it terminate the program. It's just the file handle closing that is
ensured to be done under all circumstances. And btw you can do the
similar things in Java with a finally block - it's just awfully more
verbose. And even Java has unchecked exceptions (everything that
inherits Error and RuntimeException).

I've also written another article how to write methods like File.open
yourself:
http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html

Kind regards

robert
 
C

Carl Jenkins

Robert said:
You still need to handle the exception somewhere unless you want to let
it terminate the program. It's just the file handle closing that is
ensured to be done under all circumstances. And btw you can do the
similar things in Java with a finally block - it's just awfully more
verbose. And even Java has unchecked exceptions (everything that
inherits Error and RuntimeException).

I've also written another article how to write methods like File.open
yourself:
http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html

Kind regards

robert

Alright, but I guess what I am missing is HOW do we handle the
excpetion? I see now recuse statement.

Maybe I am missing something obvious but, with Java the method signature
indicated the exception being thrown. That way in the calling method I
could deal with it. In this example while I understand that the file
handle will be closed how do you handle an exception raised?

Thanks for your help!
 
R

Robert Klemme

2010/7/2 Carl Jenkins said:
s.html

Alright, but I guess what I am missing is HOW do we handle the
excpetion? I see now recuse statement.

You have to write it yourself.
Maybe I am missing something obvious but, with Java the method signature
indicated the exception being thrown.

As I said, this is not 100% true. There are unchecked exceptions in
Java as well. Practically in Ruby the documentation should state
which exceptions can be thrown.
That way in the calling method I
could deal with it. In this example while I understand that the file
handle will be closed how do you handle an exception raised?

You catch it and do whatever is appropriate. Did you read a tutorial
about Ruby and how to handle exceptions in this language? If not, see
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top