[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Customing the Exception class

Berger, Daniel

5/13/2005 9:05:00 PM

Hi all,

I have a program where I want to send all errors to a file. So, I did this:

class Exception
alias :old_init :initialize
def initialize(arg)
old_init(arg)
File.open("log.txt","a+"){ |fh| fh.puts("Error: #{arg}") }
end
end

This works for most errors, but it doesn't seem to work when the error
is a SystemCallError.

For example, this writes to the log as expected:

print "Some crazy division: "
p 1/0

But this does not:

File.open("non-existant file")

The latter raises an Errno::ENOENT.

Why, if SystemCallError is a subclass of Exception, does my approach not
work? How do I make this work as expected?

Regards,

Dan


5 Answers

Ara.T.Howard

5/13/2005 9:17:00 PM

0

Eric Hodel

5/13/2005 10:32:00 PM

0

On 13 May 2005, at 14:35, Ara.T.Howard@noaa.gov wrote:

> On Sat, 14 May 2005, Daniel Berger wrote:
>
>
>> Hi all,
>>
>> I have a program where I want to send all errors to a file. So, I
>> did this:

[snip]

>> This works for most errors, but it doesn't seem to work when the
>> error
>> is a SystemCallError.
>>
>> For example, this writes to the log as expected:
>>
>> print "Some crazy division: "
>> p 1/0
>>
>> But this does not:
>>
>> File.open("non-existant file")
>>
>> The latter raises an Errno::ENOENT.
>>
>> Why, if SystemCallError is a subclass of Exception, does my
>> approach not
>> work? How do I make this work as expected?
>
> maybe new?

[snip]

> not sure why...

SystemCallError does not use Exception's initialize:

rb_eSystemCallError = rb_define_class("SystemCallError",
rb_eStandardError);
rb_define_method(rb_eSystemCallError, "initialize",
syserr_initialize, -1);

--
Eric Hodel - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04



djberg96

5/14/2005 1:27:00 AM

0

Eric Hodel wrote:
> On 13 May 2005, at 14:35, Ara.T.Howard@noaa.gov wrote:
>
> > On Sat, 14 May 2005, Daniel Berger wrote:
> >
> >
> >> Hi all,
> >>
> >> I have a program where I want to send all errors to a file. So, I

> >> did this:
>
> [snip]
>
> >> This works for most errors, but it doesn't seem to work when the
> >> error
> >> is a SystemCallError.
> >>
> >> For example, this writes to the log as expected:
> >>
> >> print "Some crazy division: "
> >> p 1/0
> >>
> >> But this does not:
> >>
> >> File.open("non-existant file")
> >>
> >> The latter raises an Errno::ENOENT.
> >>
> >> Why, if SystemCallError is a subclass of Exception, does my
> >> approach not
> >> work? How do I make this work as expected?
> >
> > maybe new?
>
> [snip]
>
> > not sure why...
>
> SystemCallError does not use Exception's initialize:
>
> rb_eSystemCallError = rb_define_class("SystemCallError",
> rb_eStandardError);
> rb_define_method(rb_eSystemCallError, "initialize",
> syserr_initialize, -1);

Yuck. Could it be altered to call super? Or would that cause some
hideous side effect?

Regards

Dan

PS - Sorry if this is a double post - google groups went goofy on me
for a minute.

nobu.nokada

5/14/2005 2:35:00 AM

0

Hi,

At Sat, 14 May 2005 10:30:29 +0900,
Daniel Berger wrote in [ruby-talk:142610]:
> > SystemCallError does not use Exception's initialize:
> >
> > rb_eSystemCallError = rb_define_class("SystemCallError",
> > rb_eStandardError);
> > rb_define_method(rb_eSystemCallError, "initialize",
> > syserr_initialize, -1);

Accurately, syserr_initialize() directly calls exc_initialize()
which implements Execption#initialize.

> Yuck. Could it be altered to call super? Or would that cause some
> hideous side effect?

Just for efficiency, I guess.


Index: error.c
===================================================================
RCS file: /cvs/ruby/src/ruby/error.c,v
retrieving revision 1.107
diff -U2 -p -r1.107 error.c
--- error.c 18 Mar 2005 03:17:27 -0000 1.107
+++ error.c 14 May 2005 02:32:25 -0000
@@ -387,5 +387,5 @@ exc_exception(argc, argv, self)
if (argc == 1 && self == argv[0]) return self;
exc = rb_obj_clone(self);
- exc_initialize(argc, argv, exc);
+ rb_obj_call_init(exc, argc, argv);

return exc;
@@ -581,5 +581,5 @@ exit_initialize(argc, argv, exc)
--argc;
}
- exc_initialize(argc, argv, exc);
+ rb_call_super(argc, argv);
rb_iv_set(exc, "status", status);
return exc;
@@ -661,5 +661,5 @@ name_err_initialize(argc, argv, self)

name = (argc > 1) ? argv[--argc] : Qnil;
- exc_initialize(argc, argv, self);
+ rb_call_super(argc, argv);
rb_iv_set(self, "name", name);
return self;
@@ -966,5 +966,5 @@ syserr_initialize(argc, argv, self)
mesg = rb_str_new2(err);
}
- exc_initialize(1, &mesg, self);
+ rb_call_super(1, &mesg);
rb_iv_set(self, "errno", error);
return self;


--
Nobu Nakada


djberg96

5/14/2005 2:21:00 PM

0

nobu.nok...@softhome.net wrote:
> Hi,
>
> At Sat, 14 May 2005 10:30:29 +0900,
> Daniel Berger wrote in [ruby-talk:142610]:
> > > SystemCallError does not use Exception's initialize:
> > >
> > > rb_eSystemCallError = rb_define_class("SystemCallError",
> > > rb_eStandardError);
> > > rb_define_method(rb_eSystemCallError, "initialize",
> > > syserr_initialize, -1);
>
> Accurately, syserr_initialize() directly calls exc_initialize()
> which implements Execption#initialize.
>
> > Yuck. Could it be altered to call super? Or would that cause some
> > hideous side effect?
>
> Just for efficiency, I guess.
>
>
> Index: error.c
> ===================================================================
> RCS file: /cvs/ruby/src/ruby/error.c,v
> retrieving revision 1.107
> diff -U2 -p -r1.107 error.c
> --- error.c 18 Mar 2005 03:17:27 -0000 1.107
> +++ error.c 14 May 2005 02:32:25 -0000
> @@ -387,5 +387,5 @@ exc_exception(argc, argv, self)
> if (argc == 1 && self == argv[0]) return self;
> exc = rb_obj_clone(self);
> - exc_initialize(argc, argv, exc);
> + rb_obj_call_init(exc, argc, argv);
>
> return exc;
> @@ -581,5 +581,5 @@ exit_initialize(argc, argv, exc)
> --argc;
> }
> - exc_initialize(argc, argv, exc);
> + rb_call_super(argc, argv);
> rb_iv_set(exc, "status", status);
> return exc;
> @@ -661,5 +661,5 @@ name_err_initialize(argc, argv, self)
>
> name = (argc > 1) ? argv[--argc] : Qnil;
> - exc_initialize(argc, argv, self);
> + rb_call_super(argc, argv);
> rb_iv_set(self, "name", name);
> return self;
> @@ -966,5 +966,5 @@ syserr_initialize(argc, argv, self)
> mesg = rb_str_new2(err);
> }
> - exc_initialize(1, &mesg, self);
> + rb_call_super(1, &mesg);
> rb_iv_set(self, "errno", error);
> return self;
>
>
> --
> Nobu Nakada

Will this patch be applied to the 1.8 branch? I'd like to see it in
1.8.3 but I understand if you'd rather merge it into 1.9 instead.

Regards,

Dan