Ignore a especific exception

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Ignore a especific exception

Fernando Rodriguez
Hi,

I'm using #withAllSubclassesDo: [ :each | each scheme] on a Url class
to create an OrderedCollection of schemes.

However, some of the subclasses of Url migth raise an exception
(either shouldNotImplement or notYetImplemented) when receiving the
message #scheme.

How do I ignore those 2 specific exceptions and continue with the next
subclass?

Thanks


Reply | Threaded
Open this post in threaded view
|

Re: Ignore a especific exception

Esteban A. Maringolo-3
Fernando Rodriguez escribió:

> Hi,
>
> I'm using #withAllSubclassesDo: [ :each | each scheme] on a Url class
> to create an OrderedCollection of schemes.
>
> However, some of the subclasses of Url migth raise an exception
> (either shouldNotImplement or notYetImplemented) when receiving the
> message #scheme.
>
> How do I ignore those 2 specific exceptions and continue with the next
> subclass?

You can't. At least no specifically.

You should do standard exception handling

YourClass withAllSubclassesDo: [ :each | [each scheme] on: Error
do: [:ex | ex return]
]

Best regards.


Reply | Threaded
Open this post in threaded view
|

Re: Ignore a especific exception

ChanHong Kim
In reply to this post by Fernando Rodriguez
You shoud send message  #on:do: to a block that is your code.

[
  "...some your code..."
] on: Error do: [ :e |
  "...nothing..."
]

However, this is very dangerouse way because
other critical error may be occured from your code.

In DOlphin, #shouldNotImplement  and #notYetImplemented message don't
throw specific Exception. If you want to do so, you may modify
standard(from OB) code of theese method. However, you may encounter
'compatibility issus'.

Regards!


Reply | Threaded
Open this post in threaded view
|

Re: Ignore a especific exception

Chris Uppal-3
In reply to this post by Fernando Rodriguez
Fernando,

> I'm using #withAllSubclassesDo: [ :each | each scheme] on a Url class
> to create an OrderedCollection of schemes.
>
> However, some of the subclasses of Url migth raise an exception
> (either shouldNotImplement or notYetImplemented) when receiving the
> message #scheme.

Instead of sending #scheme unconditionally, and attempting to recover from the
resulting errors, you might like to try a different approach.  Here's one I've
used and been quite happy with:

You use a loop like:

    ... allSubclasses: [ :each | each doSomethingWithScheme].

where #doSomethingWithScheme is:

    doSomethingWithScheme
        self isRealScheme ifTrue: ["code using" self scheme].

And -- this is the trick that makes it work -- the /default/ implementation of
#hasRealScheme is:

    isRealScheme
        ^ self class includesSelector: #scheme.

That means that, by default, only subclasses that provide their own
implementation of #scheme (rather than inheriting the #subclassResponsibility
implementation from the base class) will be considered to be candidates.   That
default won't be good enough for subclasses that have #shouldNotImplement
overrides of #scheme, but (I'd expect) that wouldn't be the typical case.  And,
anyway, those classes can always explicitly override #isRealScheme to answer
false.

There are several possible elaborations.  You could -- just as an example --
make Schemes that are still 'under development' answer true to #isRealScheme
only in images running on you development machine.

    -- chris