[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

"ensure" hiding actual error

Nit Khair

10/1/2008 7:34:00 AM

In my main program I have a "begin ensure end".
The "ensure" releases some resources.

If there is a syntax error somewhere deep, the program comes to 'ensure'
and shows me an error in the 'ensure', not the original error. Only if I
block out the 'ensure', does the actual error show.

I tried "rescue" with Exception, StandardError and ScriptError to print
the exception, but flow never reaches the 'rescue' block, it jumps ahead
to "ensure".
--
Posted via http://www.ruby-....

18 Answers

Robert Klemme

10/1/2008 11:45:00 AM

0

2008/10/1 Nit Khair <sentinel.2001@gmx.com>:
> In my main program I have a "begin ensure end".
> The "ensure" releases some resources.
>
> If there is a syntax error somewhere deep, the program comes to 'ensure'
> and shows me an error in the 'ensure', not the original error. Only if I
> block out the 'ensure', does the actual error show.
>
> I tried "rescue" with Exception, StandardError and ScriptError to print
> the exception, but flow never reaches the 'rescue' block, it jumps ahead
> to "ensure".

This sounds strange because syntax errors should be caught earlier -
unless they stem from an "require" or "load". Can you post a short
example that reproduces the error?

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end

Nit Khair

10/1/2008 12:09:00 PM

0

Robert Klemme wrote:

>
> This sounds strange because syntax errors should be caught earlier -
> unless they stem from an "require" or "load". Can you post a short
> example that reproduces the error?
>
> Kind regards
>
> robert

Could you elaborate on that? At the moment, I am requiring a Module and
inheriting from a class. And the syntax error was in one of them.
However, as i said, when i remove the 'ensure' it correctly shows the
error in the relevant class.

I cannot recall if this error occurred when i had everything in one.
Will try to give a short sample. It's an ncurses-ruby program.
--
Posted via http://www.ruby-....

Robert Klemme

10/1/2008 1:20:00 PM

0

2008/10/1 Nit Khair <sentinel.2001@gmx.com>:
> Robert Klemme wrote:
>
>> This sounds strange because syntax errors should be caught earlier -
>> unless they stem from an "require" or "load". Can you post a short
>> example that reproduces the error?
>
> Could you elaborate on that?

When Ruby interpreter loads a file it checks syntax (you can do
yourself with "ruby -c <script>"). If there is a syntax error that
will be shown before execution starts at all. If you require a file
and there is a syntax error in that there you will see an exception
stemming from the 'require':

1. error in main, 2. error in required:

15:20:13 OPSC_Gold_bas_dev_R1.2.3$ ruby -e 'puts 111+; require
ARGV.shift' <(echo /)
-e:1: syntax error, unexpected ';'
puts 111+; require ARGV.shift
^
15:20:22 OPSC_Gold_bas_dev_R1.2.3$ ruby -e 'puts 111; require
ARGV.shift' <(echo /)
111
-e:1:in `require': /dev/fd/63.rb:1: unterminated string meets end of
file (SyntaxError)
/dev/fd/63.rb:1: syntax error, unexpected tSTRING_END, expecting
tSTRING_CONTENT or tREGEXP_END or tSTRING_DBEG or tSTRING_DVAR
from -e:1
15:20:26 OPSC_Gold_bas_dev_R1.2.3$

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end

Nit Khair

10/1/2008 1:48:00 PM

0

Robert Klemme wrote:
> 2008/10/1 Nit Khair <sentinel.2001@gmx.com>:
>> Robert Klemme wrote:
>>
>>> This sounds strange because syntax errors should be caught earlier -
>>> unless they stem from an "require" or "load". Can you post a short
>>> example that reproduces the error?
>>

I've tried to make a trivial case. Variable x is not initialized.

#!/usr/bin/env ruby -w

begin
@test = x


ensure
@test.strip
end

The error I get is:

x.rb:8: warning: instance variable @test not initialized
x.rb:8: undefined method `strip' for nil:NilClass (NoMethodError)


Now in real life, "@test = " calls a method elsewhere which may call
other methods. So i am clueless as to where the error actually is.

Now let's comment the ensure word. The new error message is:

x.rb:4: undefined local variable or method `x' for main:Object
(NameError)

So this is currently my only solution.
In this particular case, I now add (before the ensure):

rescue Exception => boom
$stderr.print boom

and now I get the error. However, in my real curses program, it skips
the rescue clause and continues to go into the ensure.
--
Posted via http://www.ruby-....

TPReal

10/1/2008 1:53:00 PM

0

Nit Khair wrote:
> Now in real life, "@test = " calls a method elsewhere which may call
> other methods. So i am clueless as to where the error actually is.

You caused another exception inside your ensure clause, so the previous
one got lost, so that's your mistake. If you want the original exception
to live to tell the tale, you cannot raise another in your ensure
clause. But if your ensure clause was correct, there wouldn't be any
problems with handling correctly the original exception.

TPR.
--
Posted via http://www.ruby-....

Robert Klemme

10/1/2008 2:05:00 PM

0

2008/10/1 Thomas B. <tpreal@gmail.com>:
> Nit Khair wrote:
>> Now in real life, "@test = " calls a method elsewhere which may call
>> other methods. So i am clueless as to where the error actually is.
>
> You caused another exception inside your ensure clause, so the previous
> one got lost, so that's your mistake. If you want the original exception
> to live to tell the tale, you cannot raise another in your ensure
> clause. But if your ensure clause was correct, there wouldn't be any
> problems with handling correctly the original exception.

Adding to that: an uninitialized variable does not cause a syntax
error. IIRC initially this was about a syntax error.

Cheers

robert


--
remember.guy do |as, often| as.you_can - without end

Nit Khair

10/1/2008 3:08:00 PM

0

Robert Klemme wrote:
> 2008/10/1 Thomas B. <tpreal@gmail.com>:
>> Nit Khair wrote:
>>> Now in real life, "@test = " calls a method elsewhere which may call
>>> other methods. So i am clueless as to where the error actually is.
>>
>> You caused another exception inside your ensure clause, so the previous
>> one got lost, so that's your mistake. If you want the original exception
>> to live to tell the tale, you cannot raise another in your ensure
>> clause. But if your ensure clause was correct, there wouldn't be any
>> problems with handling correctly the original exception.
>

I am not following you. How have i caused another exception in the
ensure clause. I am not raising an exception in the ensure. The ensure
clause only has some statement to free resources. as in :


begin
my_form = create_....

ensure
my_form.free_form.
...
end


In the example i gave you, the stderr.print is printing the error from
above, but in my original ncurses program, it seems to directly go to
the ensure.

Let's say in my ensure I said:

my_form.free_form if !my_form.nil?
then the program would just terminate silently.


> Adding to that: an uninitialized variable does not cause a syntax
> error. IIRC initially this was about a syntax error.
>

Okay, i was trying to give you a simple example. Sometimes it is a
variable - i may misspell it, or in some cases it is a syntax error.

Even currently as I am refactoring my code, I am sometimes getting the
correct error, sometimes I have to comment out "ensure" to get the
error.

> Cheers
>
> robert

Perhaps I should ask the question differently. Am i doing something
wrong, am i using the ensure clause incorrectly (the samples I started
with released these structures in the ensure). This is my first major
ruby app.
Thanks a lot for your time, Robert!
--
Posted via http://www.ruby-....

Glen Holcomb

10/1/2008 3:22:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Wed, Oct 1, 2008 at 9:08 AM, Nit Khair <sentinel.2001@gmx.com> wrote:

> Robert Klemme wrote:
> > 2008/10/1 Thomas B. <tpreal@gmail.com>:
> >> Nit Khair wrote:
> >>> Now in real life, "@test = " calls a method elsewhere which may call
> >>> other methods. So i am clueless as to where the error actually is.
> >>
> >> You caused another exception inside your ensure clause, so the previous
> >> one got lost, so that's your mistake. If you want the original exception
> >> to live to tell the tale, you cannot raise another in your ensure
> >> clause. But if your ensure clause was correct, there wouldn't be any
> >> problems with handling correctly the original exception.
> >
>
> I am not following you. How have i caused another exception in the
> ensure clause. I am not raising an exception in the ensure. The ensure
> clause only has some statement to free resources. as in :
>
>
> begin
> my_form = create_....
>
> ensure
> my_form.free_form.
> ...
> end
>

What happens here if my_form never got a value because there was an error
previous that made create_... invalid?

What Thomas is saying is that there is an exception being raised by the code
present in your ensure block.


>
>
> In the example i gave you, the stderr.print is printing the error from
> above, but in my original ncurses program, it seems to directly go to
> the ensure.
>
> Let's say in my ensure I said:
>
> my_form.free_form if !my_form.nil?
> then the program would just terminate silently.
>
>
> > Adding to that: an uninitialized variable does not cause a syntax
> > error. IIRC initially this was about a syntax error.
> >
>
> Okay, i was trying to give you a simple example. Sometimes it is a
> variable - i may misspell it, or in some cases it is a syntax error.
>
> Even currently as I am refactoring my code, I am sometimes getting the
> correct error, sometimes I have to comment out "ensure" to get the
> error.
>
> > Cheers
> >
> > robert
>
> Perhaps I should ask the question differently. Am i doing something
> wrong, am i using the ensure clause incorrectly (the samples I started
> with released these structures in the ensure). This is my first major
> ruby app.
> Thanks a lot for your time, Robert!
> --
> Posted via http://www.ruby-....
>
>


--
"Hey brother Christian with your high and mighty errand, Your actions speak
so loud, I can't hear a word you're saying."

-Greg Graffin (Bad Religion)

Nit Khair

10/1/2008 3:32:00 PM

0

Glen Holcomb wrote:
> On Wed, Oct 1, 2008 at 9:08 AM, Nit Khair <sentinel.2001@gmx.com> wrote:
>
>> >> problems with handling correctly the original exception.
>> ensure
>> my_form.free_form.
>> ...
>> end
>>
>
> What happens here if my_form never got a value because there was an
> error
> previous that made create_... invalid?
>
> What Thomas is saying is that there is an exception being raised by the
> code
> present in your ensure block.
>
>
Fine. I get that. So is there a better or correct way of doing this so
that I get the original error that was raised.

Aaaah. so you mean if i check for my_form before freeing, then ... since
no new exception is raised, the original exception will be displayed.
So I _should_ say:

ensure
if !my_form.nil?
my_form.free ... etc
end
--
Posted via http://www.ruby-....

TPReal

10/1/2008 3:34:00 PM

0

Nit Khair wrote:
> Robert Klemme wrote:
> I am not following you. How have i caused another exception in the
> ensure clause. I am not raising an exception in the ensure. The ensure
> clause only has some statement to free resources. as in :

In the example you gave, where your ensure clause is @test.strip, you
did raise another exception, the "undefined method `strip' for
nil:NilClass (NoMethodError)" namely. Your ensure clause should do its
best not to raise any exception, or else the original exception is lost,
as you see.

> Perhaps I should ask the question differently. Am i doing something
> wrong, am i using the ensure clause incorrectly (the samples I started
> with released these structures in the ensure). This is my first major
> ruby app.

You are using the ensure correctly. The problem is that under some
circumstances the code in your ensure clause raises an exception. Maybe
you should use a separate begin rescue end inside your ensure clause.

TPR.
--
Posted via http://www.ruby-....