Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Epsilon » [eol][evl]Handling exception in casting types(Is there a way to handle exception in an operation ?)
[eol][evl]Handling exception in casting types [message #763322] Fri, 09 December 2011 15:09 Go to next message
Paulo Alexandre is currently offline Paulo Alexandre
Messages: 17
Registered: November 2011
Junior Member
hi everybody

I have a class with a <<value>> string field. The purpose of this field is to receive any input data (integers, booleans, reals and strings) and then casting to the desiredtype (like Integer, Boolean, Real ...).

The casting (conversion) of this <<value>> field is happening in a .evl file.

In EVL i'm trying to know if the content of <<value>> field is consistent with the choosen type.

for instance
<<value>> field | choosen type
true boolean It's ok!
Today Integer It isn't ok because ''today'' is not a integer

To do that, I've tried this (to convert from String to Integer)

check: self.valor.asInteger().isKindOf(Integer)

when <<value>> is an Integer, this works well but it isn't this piece of code raises an exception and I couldn't find a way to handling exception in eol or evl.

I'd appreciate any help.

Thanks in advance!

Re: [eol][evl]Handling exception in casting types [message #763419 is a reply to message #763322] Fri, 09 December 2011 17:11 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen Zschaler
Messages: 228
Registered: July 2009
Senior Member
Hi Paulo,

The code you have written would not check the validity of the string. If
asInteger() is successful it will, per definition, return an integer. So
calling isKindOf (Integer) on it is redundant!

You will probably need to build a helper class of your own to provide
appropriate testing and conversion methods. Check out the 'tools'
concept in the Epsilon book.

Cheers,

Steffen

On 09/12/2011 15:09, Paulo Alexandre wrote:
> hi everybody
>
> I have a class with a <<value>> string field. The purpose of this
> field is to receive any input data (integers, booleans, reals and
> strings) and then casting to the desiredtype (like Integer, Boolean,
> Real ...).
>
> The casting (conversion) of this <<value>> field is happening in a
> .evl file.
>
> In EVL i'm trying to know if the content of <<value>> field is
> consistent with the choosen type.
>
> for instance
> <<value>> field | choosen type true boolean
> It's ok!
> Today Integer It isn't ok because ''today'' is
> not a integer
>
> To do that, I've tried this (to convert from String to Integer)
>
> check: self.valor.asInteger().isKindOf(Integer)
>
> when <<value>> is an Integer, this works well but it isn't this piece
> of code raises an exception and I couldn't find a way to handling
> exception in eol or evl.
>
> I'd appreciate any help.
>
> Thanks in advance!
>
>
Re: [eol][evl]Handling exception in casting types [message #763692 is a reply to message #763419] Sat, 10 December 2011 10:29 Go to previous messageGo to next message
Antonio Garcia-Dominguez is currently offline Antonio Garcia-Dominguez
Messages: 309
Registered: January 2010
Senior Member
Hi Paulo,

Steffen is right: there is no way to check that it's really an integer from pure EVL. In Java you'd normally try to use Integer.valueOf and then check for a NumberFormatException, but we don't have anything for catching exceptions in EVL.

If performance is not an issue (you only have a few of those strings), you could approximate it with a regex. Since it's a String, you could use the matches method:

self.string.matches("[0-9]+")


However, this simple regex would have to be compiled again and again, and it wouldn't take into account all the possible variations for a Java integer literal.

Cheers,
Antonio
Re: [eol][evl]Handling exception in casting types [message #763698 is a reply to message #763692] Sat, 10 December 2011 10:55 Go to previous messageGo to next message
Dimitris Kolovos is currently offline Dimitris Kolovos
Messages: 867
Registered: July 2009
Senior Member
Actually, String provides the isInteger() and isReal() built-in operations but it seems that the book hasn't been updated accordingly (will do this shortly). E.g.

"123".isInteger() -> true
"abc".isInteger() -> false

There is no isBoolean() operation but this could be implemented as follows:

operation String isBoolean() : Boolean {
return self == "true" or self=="false";
}

Cheers,
Dimitris
Re: [eol][evl]Handling exception in casting types [message #763890 is a reply to message #763322] Sat, 10 December 2011 20:41 Go to previous messageGo to next message
Paulo Alexandre is currently offline Paulo Alexandre
Messages: 17
Registered: November 2011
Junior Member
Steffen, Antonio and Dimitris

I really thank you for all the support!

I'll try those ideas and pieces of code and I'll give you a feedbacK!

Thank you very much!!!!!
Re: [eol][evl]Handling exception in casting types [message #778723 is a reply to message #763890] Fri, 13 January 2012 17:48 Go to previous message
Paulo Alexandre is currently offline Paulo Alexandre
Messages: 17
Registered: November 2011
Junior Member
I just forgot to give a feedback...It worked!
Previous Topic:Feature xyz must be set
Next Topic:[EGL] How to set XMLResrouce options
Goto Forum:
  


Current Time: Sat Sep 20 04:02:12 GMT 2014

Powered by FUDForum. Page generated in 0.06635 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software