Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Handling File.open exceptions

Reply
Thread Tools

Handling File.open exceptions

 
 
Carl Jenkins
Guest
Posts: n/a
 
      07-01-2010
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?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      07-01-2010
On 01.07.2010 22:24, Carl Jenkins wrote:
> 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/po...obustness.html

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
 
 
 
Carl Jenkins
Guest
Posts: n/a
 
      07-02-2010
Robert Klemme wrote:
> On 01.07.2010 22:24, Carl Jenkins wrote:
>> 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/po...obustness.html
>
> Kind regards
>
> robert


Yes - thanks that does help.

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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      07-02-2010
On 07/02/2010 03:08 AM, Carl Jenkins wrote:
> Robert Klemme wrote:
>> On 01.07.2010 22:24, Carl Jenkins wrote:
>>> 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/po...obustness.html
>>

>
> 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/po...k_Methods.html

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
Carl Jenkins
Guest
Posts: n/a
 
      07-02-2010
Robert Klemme wrote:
> On 07/02/2010 03:08 AM, Carl Jenkins wrote:
>>>> block?
>>>
>>> Does this help?
>>>
>>> http://blog.rubybestpractices.com/po...obustness.html
>>>

>>
>> 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/po...k_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!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      07-02-2010
2010/7/2 Carl Jenkins <(E-Mail Removed)>:
> Robert Klemme wrote:
>> On 07/02/2010 03:08 AM, Carl Jenkins wrote:
>>>>> block?
>>>>
>>>> Does this help?
>>>>
>>>> http://blog.rubybestpractices.com/po..._blocks_for_R=

obustness.html
>>>>
>>>
>>> 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. =A0It's just the file handle closing that is
>> ensured to be done under all circumstances. =A0And btw you can do the
>> similar things in Java with a finally block - it's just awfully more
>> verbose. =A0And 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/po..._Block_Method=

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/Program...xceptions.html

Cheers

robert

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exceptions - How do you make it work like built-in exceptions? Lie Python 3 01-14-2008 06:45 PM
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
Handling Exceptions in an n-tier environment Raterus ASP .Net 6 08-24-2004 08:21 PM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments