Thread overview
dwt2 help
Mar 25, 2009
Saaa
Mar 25, 2009
Sergey Gromov
Mar 26, 2009
Saaa
Mar 26, 2009
Sergey Gromov
March 25, 2009
I filled in the Phobos implementation, but I'm not sure if it is all correct as I don't fully understand the exception handling.

module java.lang.Byte;

import java.lang.util;
import java.lang.exceptions;

version(Tango){
    static import tango.text.convert.Integer;
} else { // Phobos
    static import std.conv;
}
class Byte : ValueWrapperT!(byte) {
    public static byte parseByte( String s ){
        version(Tango){
            try{
                int res = tango.text.convert.Integer.parse( s );
                if( res < byte.min || res > byte.max ){
                    throw new NumberFormatException( "out of range" );
                }
                return res;
            }
            catch( IllegalArgumentException e ){
//How does this work? It catches e and gives it as an argument to a new
exception.
                throw new NumberFormatException( e );
            }
        } else if version(Phobos){ // Phobos
            try{
                byte res = to!(byte)(s);
                return res;
            }
            catch(Object e){ // ConvError or ConvOverflowError
//Is this the correct translation?
                throw new NumberFormatException( e );
            }
            return 0;
        }
    }
    this( byte value ){
        super( value );
    }

    public static String toString( byte i ){
        return String_valueOf(i);
    }

}
alias Byte ValueWrapperByte;


March 25, 2009
Thu, 26 Mar 2009 00:29:44 +0100, Saaa wrote:

> I filled in the Phobos implementation, but I'm not sure if it is all correct as I don't fully understand the exception handling.
> 
> [...]
>
>             catch( IllegalArgumentException e ){
> //How does this work? It catches e and gives it as an argument to a new
> exception.
>                 throw new NumberFormatException( e );
>             }

Obviously NumberFormatException can store another exception object inside.  This code effectively converts IllegalArgumentException into NumberFormatException, storing the original argument exception inside so that one can examine it later if they're curious.

> [...]
>
>             catch(Object e){ // ConvError or ConvOverflowError
> //Is this the correct translation?
>                 throw new NumberFormatException( e );
>             }

It depends on what NumberFormatException can  accept as an argument.  If it accepts an Object then it's fine.
March 26, 2009
> Obviously NumberFormatException can store another exception object inside.  This code effectively converts IllegalArgumentException into NumberFormatException, storing the original argument exception inside so that one can examine it later if they're curious.
Thanks, yes this is obvious...

>
>> [...]
>>
>>             catch(Object e){ // ConvError or ConvOverflowError
>> //Is this the correct translation?
>>                 throw new NumberFormatException( e );
>>             }
>
> It depends on what NumberFormatException can  accept as an argument.  If it accepts an Object then it's fine.

class NumberFormatException : IllegalArgumentException {
    this( String e ){
        super(e);
    }
    this( Exception e ){
        super(e.toString);
    }
}

Can Object be implicitly converted to Exception?



March 26, 2009
Thu, 26 Mar 2009 01:40:25 +0100, Saaa wrote:

>>> [...]
>>>
>>>             catch(Object e){ // ConvError or ConvOverflowError
>>> //Is this the correct translation?
>>>                 throw new NumberFormatException( e );
>>>             }
>>
>> It depends on what NumberFormatException can  accept as an argument.  If it accepts an Object then it's fine.
> 
> class NumberFormatException : IllegalArgumentException {
>     this( String e ){
>         super(e);
>     }
>     this( Exception e ){
>         super(e.toString);
>     }
> }
> 
> Can Object be implicitly converted to Exception?

No it cannot.  Object is the root of the D class hierarchy.  In D1 Exception is derived directly from Object.  In D2 Exception is derived from Throwable which in turn is derived from Object.  You can cast Exception to Object but not the other way around.

What's worse, ConvError and ConvOverflowError in D2 are derived from Error which is a separate class from Exception and cannot be cast implicitly.  I think you'd want to fix the NumberFormatException to accept Throwable for instance which is a base of both Exception and Error in D2.