catching exceptions

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

catching exceptions

abdelghani ALIDRA
Hi everyBody,

I know there is some kind of exceptions handling mechanism  in Pharo but I cant exactly  remember where to find it.
Actually, I would like to execute specific code if atRandomis called on an empty Array or Collection.
I probably can still override errorEmptyCollection but maybe there is another (less radical) way.
And by the way, Would it be quicker to use exception handling then just testing the Collection before calling atRandom.

Thanks

Abdelghani 
Reply | Threaded
Open this post in threaded view
|

Re: catching exceptions

Skip Lentz-2
Hi, you can use BlockClosure>>on:do:, like this:

[ #() atRandom ] on: Exception do: [ 1 ].

Personally, I would check the collection to be empty before calling.

On Nov 26, 2015, at 1:33 PM, abdelghani ALIDRA <[hidden email]> wrote:

Hi everyBody,

I know there is some kind of exceptions handling mechanism  in Pharo but I cant exactly  remember where to find it.
Actually, I would like to execute specific code if atRandomis called on an empty Array or Collection.
I probably can still override errorEmptyCollection but maybe there is another (less radical) way.
And by the way, Would it be quicker to use exception handling then just testing the Collection before calling atRandom.

Thanks

Abdelghani 

Reply | Threaded
Open this post in threaded view
|

RE: catching exceptions

BriceG
In reply to this post by abdelghani ALIDRA

Hi,

take a look at the method on:do: and especially its sender you'll get an hint about it.

There's different kind of Exception you can use in the package Kernel-Exceptions.

You can also use your own Exception Class I guess as long as you raise it in your code and catch it with the on:do: method.


Regards,

--------------
Brice Govin
PhD student in RMoD research team at INRIA Lille
Software Engineer at THALES AIR SYSTEMS Rungis
ENSTA-Bretagne ENSI2014

22 Avenue du General Leclerc 92340 BOURG-LA-REINE





De : abdelghani ALIDRA [via Smalltalk] <ml-node+[hidden email]>
Envoyé : jeudi 26 novembre 2015 13:18
À : Brice GOVIN
Objet : catching exceptions
 
Hi everyBody,

I know there is some kind of exceptions handling mechanism  in Pharo but I cant exactly  remember where to find it.
Actually, I would like to execute specific code if atRandomis called on an empty Array or Collection.
I probably can still override errorEmptyCollection but maybe there is another (less radical) way.
And by the way, Would it be quicker to use exception handling then just testing the Collection before calling atRandom.

Thanks

Abdelghani 



If you reply to this email, your message will be added to the discussion below:
http://forum.world.st/catching-exceptions-tp4863648.html
forum.world.st
catching exceptions. Hi everyBody, I know there is some kind of exceptions handling mechanism  in Pharo but I cant exactly  remember where to find it.Actually, I would like to execute specific code if...
To unsubscribe from Pharo Smalltalk Users, click here.
NAML
Reply | Threaded
Open this post in threaded view
|

Re: catching exceptions

abdelghani ALIDRA
In reply to this post by Skip Lentz-2
Hi Skip, 

It is working fine for me

Thank you. 

Abdelghani



De : Skip Lentz <[hidden email]>
À : abdelghani ALIDRA <[hidden email]>; Any question about pharo is welcome <[hidden email]>
Envoyé le : Jeudi 26 novembre 2015 13h38
Objet : Re: [Pharo-users] catching exceptions

Hi, you can use BlockClosure>>on:do:, like this:

[ #() atRandom ] on: Exception do: [ 1 ].

Personally, I would check the collection to be empty before calling.



On Nov 26, 2015, at 1:33 PM, abdelghani ALIDRA <[hidden email]> wrote:

Hi everyBody,

I know there is some kind of exceptions handling mechanism  in Pharo but I cant exactly  remember where to find it.
Actually, I would like to execute specific code if atRandomis called on an empty Array or Collection.
I probably can still override errorEmptyCollection but maybe there is another (less radical) way.
And by the way, Would it be quicker to use exception handling then just testing the Collection before calling atRandom.

Thanks

Abdelghani 



ksi
Reply | Threaded
Open this post in threaded view
|

Re: catching exceptions

ksi
In reply to this post by abdelghani ALIDRA
Here is a complete chapter on Exceptions and Exception Handling

http://pharobooks.gforge.inria.fr/PharoByExampleTwo-Eng/latest/Exceptions.pdf

Have fun !

Cheers
Kiril