You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by C....@ads.it on 2006/10/24 17:38:49 UTC

TypeHandlerCallback.valueOf(): exceptions


Hi all

I've just read the example about YesNoBoolTypeHandlerCallback (p. 41 of
BATIS-SqlMaps-2_en.pdf, Creating custom Type Handlers))
and the API for the  TypeHandlerCallback  interface

   public java.lang.Object valueOf(java.lang.String s)

   Casts the string representation of a value into a type recognized by
   this type handler. This method is used to translate nullValue values
   into types that can be appropriately compared. If your custom type
   handler cannot support nullValues, or if there is no reasonable string
   representation for this type (e.g. File type), you can simply return the
   String representation as it was passed in. It is not recommended to
   return null, unless null was passed in.

In the documented example a SQLException is raised when the passed string
cannot be converted to a Boolean.

Please note that

I)
The signature of  valueOf() does not mention any exception ("Unexpected
value " + s + " found where " + YES + " or " + NO + " was expected."); )

II)
If I try to raise an exception...

      public Object valueOf( String pString )
      {
            Box< RuntimeException > lBoxRuntimeException = new Box<
RuntimeException >();
            TipoBD lResult = TipoBD.newInstance( pString,
lBoxRuntimeException );
            if ( lResult == null )
            {
      [*]         throw new SQLException ( lBoxRuntimeException.toString()
);

Eclipse complains...

   Unhandled exception type SQLException

Viceversa, If I add throws SQLException the diagnostic becomes

   Exception SQLException is not compatible with throws clause in
   TypeHandlerCallback.valuefOf(String)

III)
The column on the DB cannot contain neither null values nort invalid values
(the integrity is enforced by a proper constraint on the column).
So, it should never come a null value.
Secondly, a null value cannot anyway be handled.
Should I raise an exception, but how, since the code [*] above does not
compile?
Or should I simply return the String representation as it was passed in?
(in this case either null or of any invalid value, if any)?

Please note that another example

      http://www.mail-archive.com/user-java@ibatis.apache.org/msg04873.html

contains code that does not raise any exception at all.
So might I ignore the exception?

Thanks in advance for any explanation.

ciao
Cesare

Re: TypeHandlerCallback.valueOf(): exceptions

Posted by Jeff Butler <je...@gmail.com>.
There is an error in the documentation.  There's already a JIRA issue filed
for this.

The valueOf method does not allow SqlException.  If you feel you must throw
an exception, then it should be RuntimeException.

Jeff Butler


On 10/24/06, C.Zecca@ads.it <C....@ads.it> wrote:
>
>  Hi all
>
> I've just read the example about YesNoBoolTypeHandlerCallback (p. 41 of
> BATIS-SqlMaps-2_en.pdf, Creating custom Type Handlers))
> and the API for the TypeHandlerCallback interface
>
>    public java.lang.Object valueOf(java.lang.String s)
>
>    Casts the string representation of a value into a type recognized by
>    this type handler. This method is used to translate nullValue values into
>    types that can be appropriately compared. If your custom type handler cannot
>    support nullValues, or if there is no reasonable string representation for
>    this type (e.g. File type), you can simply return the String
>    representation as it was passed in. It is not recommended to return null,
>    unless null was passed in.
>
>
> In the documented example a SQLException is raised when the passed string
> cannot be converted to a Boolean.
>
> Please note that
>
> I)
> The signature of valueOf() does not mention any exception ("Unexpected
> value " + s + " found where " + YES + " or " + NO + " was expected."); )
>
> II)
> If I try to raise an exception...
>
> public Object valueOf( String pString )
> {
> Box< RuntimeException > lBoxRuntimeException = new Box< RuntimeException
> >();
> TipoBD lResult = TipoBD.newInstance( pString, lBoxRuntimeException );
> if ( lResult == null )
> {
> [*] throw new SQLException ( lBoxRuntimeException.toString() );
>
> Eclipse complains...
>
>    Unhandled exception type SQLException
>
>
> Viceversa, If I add throws SQLException the diagnostic becomes
>
>    Exception SQLException is not compatible with throws clause in
>    TypeHandlerCallback.valuefOf(String)
>
> III)
> The column on the DB cannot contain neither null values nort invalid
> values (the integrity is enforced by a proper constraint on the column).
> So, it should never come a null value.
> Secondly, a null value cannot anyway be handled.
> Should I raise an exception, but *how*, since the code [*] above does *not
> * compile?
> Or should I simply return the String representation as it was passed in?
> (in this case either null or of any invalid value, if any)?
>
> Please note that another example
>
> http://www.mail-archive.com/user-java@ibatis.apache.org/msg04873.html
>
> contains code that does not raise any exception at all.
> So might I ignore the exception?
>
> Thanks in advance for any explanation.
>
> ciao
> Cesare
>
>