[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Custom Exception classes

Clark Snowdall

7/28/2006 8:45:00 PM

Hello,

I'm new here so please be gentle (i.e. if I'm posting in the wrong place
let me know).

At any rate the team I'm working on is cooking up a ruby app and we want
to implement some error handling by using a custom exception class
inheriting from Exception. The most important thing we want is to be
able to pass an Exception object to one of our custom exception objects
rather than just a message string and have it keep that object for the
duration. The plan is to chain these exceptions together as each raised
exception will keep a reference to its parent exception (similar to C++
and Java implementations). The problem is our exception instances seem
to be disappearing from one rescue to the next. Our best guess is that
since each exception is stored in a global variable, its getting
overwritten. But we're still testing.

If anyone has any ideas or better yet custom exception implmentations
that will allow objects to persist in excptions, that would be great.

Clark

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

8 Answers

Caleb Clausen

7/28/2006 10:18:00 PM

0

The following works for me. If you have code that doesn't work, then
please post it.

class Hell<Exception;
def initialize(parent)
@parent=parent
end
attr :parent
end

begin begin begin

raise "heck"
rescue Exception=>e
raise Hell.new(e)
end

rescue Exception=>e
$e=e
print "saw exception: #{e}, #{e.parent}\n"
raise ArgumentError
end
rescue Exception=>e
print "saw exception: #{e}\n"
puts $e
end

Eric Hodel

7/29/2006 10:57:00 PM

0

On Jul 28, 2006, at 3:17 PM, Caleb Clausen wrote:

> The following works for me. If you have code that doesn't work, then
> please post it.
>
> class Hell<Exception;

Please don't subclass Exception as a plain rescue won't catch these.
Use a more-descriptive subclass like RuntimeError or even StandardError.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...



Caleb Clausen

7/30/2006 3:47:00 AM

0

On 7/29/06, Eric Hodel <drbrain@segment7.net> wrote:
> On Jul 28, 2006, at 3:17 PM, Caleb Clausen wrote:
> Please don't subclass Exception as a plain rescue won't catch these.
> Use a more-descriptive subclass like RuntimeError or even StandardError.

Ah, good point. Thanks for the tip; it hadn't occurred to me. I've
probably got other code where I did the same thing....

Sean O'Halpin

7/31/2006 7:26:00 AM

0

On 7/29/06, Eric Hodel <drbrain@segment7.net> wrote:
> On Jul 28, 2006, at 3:17 PM, Caleb Clausen wrote:
>
> > The following works for me. If you have code that doesn't work, then
> > please post it.
> >
> > class Hell<Exception;
>
> Please don't subclass Exception as a plain rescue won't catch these.
> Use a more-descriptive subclass like RuntimeError or even StandardError.
>
> --
> Eric Hodel - drbrain@segment7.net - http://blog.se...
> This implementation is HODEL-HASH-9600 compliant
>
> http://trackmap.rob...
>
Hi Eric,

I'm sure that used to be the case but it must have changed around 1.8
(not sure exactly when):

begin
raise Exception, "oops!"
rescue
p $!
end
#=> oops! (Exception)

ruby 1.8.4 (2005-12-24) [i386-mswin32]

Regards,
Sean

Nobuyoshi Nakada

7/31/2006 7:46:00 AM

0

Hi,

At Mon, 31 Jul 2006 16:25:50 +0900,
Sean O'Halpin wrote in [ruby-talk:205143]:
> I'm sure that used to be the case but it must have changed around 1.8

AFAIK, it has never changed.

--
Nobu Nakada

Eric Hodel

7/31/2006 6:44:00 PM

0

On Jul 31, 2006, at 12:25 AM, Sean O'Halpin wrote:

> On 7/29/06, Eric Hodel <drbrain@segment7.net> wrote:
>> On Jul 28, 2006, at 3:17 PM, Caleb Clausen wrote:
>>
>> > The following works for me. If you have code that doesn't work,
>> then
>> > please post it.
>> >
>> > class Hell<Exception;
>>
>> Please don't subclass Exception as a plain rescue won't catch these.
>> Use a more-descriptive subclass like RuntimeError or even
>> StandardError.
>
> I'm sure that used to be the case but it must have changed around 1.8
> (not sure exactly when):
>
> begin
> raise Exception, "oops!"
> rescue
> p $!
> end
> #=> oops! (Exception)
>
> ruby 1.8.4 (2005-12-24) [i386-mswin32]

Are you sure?

$ ruby -ve 'begin; raise Exception; rescue; p "caught"; end'
ruby 1.8.5 (2006-07-25) [powerpc-darwin8.7.0]
-e:1: Exception (Exception)
$

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...



Sean O'Halpin

8/1/2006 12:59:00 AM

0

On 7/31/06, Eric Hodel <drbrain@segment7.net> wrote:
> On Jul 31, 2006, at 12:25 AM, Sean O'Halpin wrote:
>
> > On 7/29/06, Eric Hodel <drbrain@segment7.net> wrote:
> >> On Jul 28, 2006, at 3:17 PM, Caleb Clausen wrote:
> >>
> >> > The following works for me. If you have code that doesn't work,
> >> then
> >> > please post it.
> >> >
> >> > class Hell<Exception;
> >>
> >> Please don't subclass Exception as a plain rescue won't catch these.
> >> Use a more-descriptive subclass like RuntimeError or even
> >> StandardError.
> >
> > I'm sure that used to be the case but it must have changed around 1.8
> > (not sure exactly when):
> >
> > begin
> > raise Exception, "oops!"
> > rescue
> > p $!
> > end
> > #=> oops! (Exception)
> >
> > ruby 1.8.4 (2005-12-24) [i386-mswin32]
>
> Are you sure?
>
> $ ruby -ve 'begin; raise Exception; rescue; p "caught"; end'
> ruby 1.8.5 (2006-07-25) [powerpc-darwin8.7.0]
> -e:1: Exception (Exception)
> $
>
> --
> Eric Hodel - drbrain@segment7.net - http://blog.se...
> This implementation is HODEL-HASH-9600 compliant
>
> http://trackmap.rob...
>
No - you're right. I keep getting this mixed up :/

You have to do this to catch an Exception:

begin
raise Exception, "oops!"
rescue Exception
puts "caught"
end
#> caught

I deleted Exception from the rescue clause, re-ran it and didn't read
the output correctly.
Apologies for the noise (of chomping humble pie ;)

Regards,
Sean

Lara Neff

4/5/2013 1:28:00 AM

0

'Lickin' Ass and Fakin' Military Service' is a Proven Fraud, a Proven
Liar, a Proven Thief, and recently a Proven Malicious Poster. She is
the most prolific liar in this newsgroup and also the most prolific
thief of other's writings. She is also a Stolen Valor stain on
America.

'Lickin' Ass and Fakin' Military Service' IS the Democratic Party in
America. She is an admitted election official in VA. This is proof
positive that DemocRATs are the party of Liars, Frauds & Thieves.

Note that when you reply to a Proven Liar you encourage them to
continue lying.

[][][][][][]


The DemocRAT Hall Of Shame http://www.democrathallof... asks
"Why do you always LIE?"

[Courtesy of Buster Norris]

On Thu, 15 Nov 2012 22:14:03 -0500, Kickin' Ass and Takin' Names
<PopUlist349@hotmail.com> wrote:
>Re: Chick-Fil-A declares war on Christmas
>For many Religious Right groups, Christmas is not so much a time to

Awwwwwwwwwwwwww..................

Were you hoping we'd think YOU wrote that?????????????

HAAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!!!!!

But we already KNOW that YOU aren't smart enough to write that, and
everybody knows YOU are a Proven Liar and Proven Fraud..............

And now we know where YOU STOLE THAT:
http://www.rightwingwatch.org/content/chick-fil-a-declares-war-on...

Posted from:
The DemocRATs Hall of Shame!
http://www.democrathallof...