You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@harmony.apache.org by Richard Liang <ri...@gmail.com> on 2006/07/06 14:51:59 UTC

[classlib] Exception throwing compatibility: java.util.Scanner

Hello All,

When I'm trying to implement Scanner.nextInt(int radix), I met a problem.

As we all know, Character.MIN_RADIX equals 2 and Character.MAX_RADIX 
equals 36, so the parameter radix can not less than 2 or greater than 
36, Otherwise that parameter is illegal.

But on RI, when the parameter radix is illegal, there are different 
kinds of Exception thrown.

    * If the parameter is less than 0 or greater than 36, RI throws a
      StringIndexOutOfBoundsException. Obviously this exception depends
      an RI's implementation.
    * If the parameter equals 1, RI throws an InputMismatchException
      with an additional information "The radix 1 is less than the
      Character.MIN_RADIX".
    * If the parameter equals 0, RI throws a
      java.util.regex.PatternSyntaxException whose constructor has three
      parameters. And the parameters depends on RI's implementation,
      that makes me can not follow RI.

And nothing is documented in the spec. Shall I follow RI's behavior? 
Thanks a lot.

Here is the test case to demo this issue.

public void test_nextIntI(){
       Scanner s = new Scanner("123 456");

       try {
            s.nextInt(-1);
            fail("Should throw StringIndexOutOfBoundsException");
        } catch (StringIndexOutOfBoundsException e) {
            // Expected
        }
        try {
            s.nextInt(0);
            fail("Should throw PatternSyntaxException");
        } catch (PatternSyntaxException e) {
            // Expected
        }
        try {
            s.nextInt(1);
            fail("Should throw InputMismatchException");
        } catch (InputMismatchException e) {
            // Expected
        }
        try {
            s.nextInt(40);
            fail("Should throw StringIndexOutOfBoundsException");
        } catch (StringIndexOutOfBoundsException e) {
            // Expected
        }
}

-- 
Richard Liang
China Software Development Lab, IBM 


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Paulex Yang <pa...@gmail.com>.
Richard Liang wrote:
>
>
> Paulex Yang wrote:
>> Richard Liang wrote:
>>> Hello All,
>>>
>>> When I'm trying to implement Scanner.nextInt(int radix), I met a 
>>> problem.
>>>
>>> As we all know, Character.MIN_RADIX equals 2 and Character.MAX_RADIX 
>>> equals 36, so the parameter radix can not less than 2 or greater 
>>> than 36, Otherwise that parameter is illegal.
>>>
>>> But on RI, when the parameter radix is illegal, there are different 
>>> kinds of Exception thrown.
>>>
>>>    * If the parameter is less than 0 or greater than 36, RI throws a
>>>      StringIndexOutOfBoundsException. Obviously this exception depends
>>>      an RI's implementation.
>>>    * If the parameter equals 1, RI throws an InputMismatchException
>>>      with an additional information "The radix 1 is less than the
>>>      Character.MIN_RADIX".
>>>    * If the parameter equals 0, RI throws a
>>>      java.util.regex.PatternSyntaxException whose constructor has three
>>>      parameters. And the parameters depends on RI's implementation,
>>>      that makes me can not follow RI.
>> Is this exception thrown by RegEx class? Should we identify at first 
>> if our regular expression has different behavior with RI on some 
>> certain input parameter?
> Hello Paulex,
>
> Yes, this exception is thrown by regex, but it depends on RI's 
> implementation. I don't know how to get the "input parameter", any 
> comments?
Oops, so just forget what I said:). Sorry if I made confusion, I didn't 
look closely, just wondering why the regex exception is thrown from 
Scanner, and I thought it may be straightforward to separately test the 
regex API invocation, probably I'm wrong. 
>
> Richard
>>>
>>> And nothing is documented in the spec. Shall I follow RI's behavior? 
>>> Thanks a lot.
>>>
>>> Here is the test case to demo this issue.
>>>
>>> public void test_nextIntI(){
>>>       Scanner s = new Scanner("123 456");
>>>
>>>       try {
>>>            s.nextInt(-1);
>>>            fail("Should throw StringIndexOutOfBoundsException");
>>>        } catch (StringIndexOutOfBoundsException e) {
>>>            // Expected
>>>        }
>>>        try {
>>>            s.nextInt(0);
>>>            fail("Should throw PatternSyntaxException");
>>>        } catch (PatternSyntaxException e) {
>>>            // Expected
>>>        }
>>>        try {
>>>            s.nextInt(1);
>>>            fail("Should throw InputMismatchException");
>>>        } catch (InputMismatchException e) {
>>>            // Expected
>>>        }
>>>        try {
>>>            s.nextInt(40);
>>>            fail("Should throw StringIndexOutOfBoundsException");
>>>        } catch (StringIndexOutOfBoundsException e) {
>>>            // Expected
>>>        }
>>> }
>>>
>>
>>
>


-- 
Paulex Yang
China Software Development Lab
IBM



---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Richard Liang <ri...@gmail.com>.

Paulex Yang wrote:
> Richard Liang wrote:
>> Hello All,
>>
>> When I'm trying to implement Scanner.nextInt(int radix), I met a 
>> problem.
>>
>> As we all know, Character.MIN_RADIX equals 2 and Character.MAX_RADIX 
>> equals 36, so the parameter radix can not less than 2 or greater than 
>> 36, Otherwise that parameter is illegal.
>>
>> But on RI, when the parameter radix is illegal, there are different 
>> kinds of Exception thrown.
>>
>>    * If the parameter is less than 0 or greater than 36, RI throws a
>>      StringIndexOutOfBoundsException. Obviously this exception depends
>>      an RI's implementation.
>>    * If the parameter equals 1, RI throws an InputMismatchException
>>      with an additional information "The radix 1 is less than the
>>      Character.MIN_RADIX".
>>    * If the parameter equals 0, RI throws a
>>      java.util.regex.PatternSyntaxException whose constructor has three
>>      parameters. And the parameters depends on RI's implementation,
>>      that makes me can not follow RI.
> Is this exception thrown by RegEx class? Should we identify at first 
> if our regular expression has different behavior with RI on some 
> certain input parameter?
Hello Paulex,

Yes, this exception is thrown by regex, but it depends on RI's 
implementation. I don't know how to get the "input parameter", any comments?

Richard
>>
>> And nothing is documented in the spec. Shall I follow RI's behavior? 
>> Thanks a lot.
>>
>> Here is the test case to demo this issue.
>>
>> public void test_nextIntI(){
>>       Scanner s = new Scanner("123 456");
>>
>>       try {
>>            s.nextInt(-1);
>>            fail("Should throw StringIndexOutOfBoundsException");
>>        } catch (StringIndexOutOfBoundsException e) {
>>            // Expected
>>        }
>>        try {
>>            s.nextInt(0);
>>            fail("Should throw PatternSyntaxException");
>>        } catch (PatternSyntaxException e) {
>>            // Expected
>>        }
>>        try {
>>            s.nextInt(1);
>>            fail("Should throw InputMismatchException");
>>        } catch (InputMismatchException e) {
>>            // Expected
>>        }
>>        try {
>>            s.nextInt(40);
>>            fail("Should throw StringIndexOutOfBoundsException");
>>        } catch (StringIndexOutOfBoundsException e) {
>>            // Expected
>>        }
>> }
>>
>
>

-- 
Richard Liang
China Software Development Lab, IBM 



---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Paulex Yang <pa...@gmail.com>.
Richard Liang wrote:
> Hello All,
>
> When I'm trying to implement Scanner.nextInt(int radix), I met a problem.
>
> As we all know, Character.MIN_RADIX equals 2 and Character.MAX_RADIX 
> equals 36, so the parameter radix can not less than 2 or greater than 
> 36, Otherwise that parameter is illegal.
>
> But on RI, when the parameter radix is illegal, there are different 
> kinds of Exception thrown.
>
>    * If the parameter is less than 0 or greater than 36, RI throws a
>      StringIndexOutOfBoundsException. Obviously this exception depends
>      an RI's implementation.
>    * If the parameter equals 1, RI throws an InputMismatchException
>      with an additional information "The radix 1 is less than the
>      Character.MIN_RADIX".
>    * If the parameter equals 0, RI throws a
>      java.util.regex.PatternSyntaxException whose constructor has three
>      parameters. And the parameters depends on RI's implementation,
>      that makes me can not follow RI.
Is this exception thrown by RegEx class? Should we identify at first if 
our regular expression has different behavior with RI on some certain 
input parameter?
>
> And nothing is documented in the spec. Shall I follow RI's behavior? 
> Thanks a lot.
>
> Here is the test case to demo this issue.
>
> public void test_nextIntI(){
>       Scanner s = new Scanner("123 456");
>
>       try {
>            s.nextInt(-1);
>            fail("Should throw StringIndexOutOfBoundsException");
>        } catch (StringIndexOutOfBoundsException e) {
>            // Expected
>        }
>        try {
>            s.nextInt(0);
>            fail("Should throw PatternSyntaxException");
>        } catch (PatternSyntaxException e) {
>            // Expected
>        }
>        try {
>            s.nextInt(1);
>            fail("Should throw InputMismatchException");
>        } catch (InputMismatchException e) {
>            // Expected
>        }
>        try {
>            s.nextInt(40);
>            fail("Should throw StringIndexOutOfBoundsException");
>        } catch (StringIndexOutOfBoundsException e) {
>            // Expected
>        }
> }
>


-- 
Paulex Yang
China Software Development Lab
IBM



---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Anton Luht <an...@gmail.com>.
> It seems like the RI is doing random crap, and it wouldn't be something
> that someone would depend on... can you imagine?

Easily :) One more example:

Pattern.compile("(?<![^\\xB5])", Pattern.CASE_INSENSITIVE);

exposes implementation problems:

java.lang.ArrayIndexOutOfBoundsException
        at java.util.regex.Pattern$BitClass.add(Pattern.java:2861)
.....

though it works without Pattern.CASE_INSENSITIVE

-- 
Regards,
Anton Luht,
Intel Middleware Products Division

---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Anton Avtamonov <an...@gmail.com>.
On 7/7/06, Richard Liang <ri...@gmail.com> wrote:
<SNIP>
> We may frequently encounter this confused situation, and I suggest we
> discuss the problems case by case if someone is not sure how to do. ;-)
>
> For this case, I decide to follow "useRadix(int radix)". Please correct
> me if I'm wrong. Thanks a lot.

+1.

-- 
Anton Avtamonov,
Intel Middleware Products Division

---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Richard Liang <ri...@gmail.com>.

Richard Liang wrote:
>
>
> Geir Magnusson Jr wrote:
>> Richard Liang wrote:
>>  
>>> Geir Magnusson Jr wrote:
>>>    
>>>> Richard Liang wrote:
>>>>
>>>>  
>>>>      
>>>>> For this case, I decide to follow "useRadix(int radix)". Please 
>>>>> correct
>>>>> me if I'm wrong. Thanks a lot.
>>>>>
>>>>>             
>>>> Do you mean that you're going to follow the spec?  If so, we should 
>>>> note
>>>> that we're making a conscious decision to differ from the RI.
>>>>
>>>>         
>>> Yes, Geir. For this case I think we have to differ from RI.
>>>     
>>
>> I agree.  I just had no clue what your post meant.
>>
>> And we'll document?  :)
>>   
> Thanks a lot, Geir. I will raise a "Non-bug differences from RI" issue 
> on Harmony JIRA.
>
Hello All,

I have raised the "Non-bug differences from RI" issue for this thread: 
http://issues.apache.org/jira/browse/HARMONY-858

Best regards,
Richard.
> Richard
>> geir
>>
>>
>> ---------------------------------------------------------------------
>> Terms of use : http://incubator.apache.org/harmony/mailing.html
>> To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
>> For additional commands, e-mail: harmony-dev-help@incubator.apache.org
>>
>>
>>   
>

-- 
Richard Liang
China Software Development Lab, IBM 



---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Richard Liang <ri...@gmail.com>.

Geir Magnusson Jr wrote:
> Richard Liang wrote:
>   
>> Geir Magnusson Jr wrote:
>>     
>>> Richard Liang wrote:
>>>
>>>  
>>>       
>>>> For this case, I decide to follow "useRadix(int radix)". Please correct
>>>> me if I'm wrong. Thanks a lot.
>>>>
>>>>     
>>>>         
>>> Do you mean that you're going to follow the spec?  If so, we should note
>>> that we're making a conscious decision to differ from the RI.
>>>
>>>   
>>>       
>> Yes, Geir. For this case I think we have to differ from RI.
>>     
>
> I agree.  I just had no clue what your post meant.
>
> And we'll document?  :)
>   
Thanks a lot, Geir. I will raise a "Non-bug differences from RI" issue 
on Harmony JIRA.

Richard
> geir
>
>
> ---------------------------------------------------------------------
> Terms of use : http://incubator.apache.org/harmony/mailing.html
> To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
> For additional commands, e-mail: harmony-dev-help@incubator.apache.org
>
>
>   

-- 
Richard Liang
China Software Development Lab, IBM 


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Geir Magnusson Jr <ge...@pobox.com>.

Richard Liang wrote:
> 
> 
> Geir Magnusson Jr wrote:
>> Richard Liang wrote:
>>
>>  
>>> For this case, I decide to follow "useRadix(int radix)". Please correct
>>> me if I'm wrong. Thanks a lot.
>>>
>>>     
>>
>> Do you mean that you're going to follow the spec?  If so, we should note
>> that we're making a conscious decision to differ from the RI.
>>
>>   
> Yes, Geir. For this case I think we have to differ from RI.

I agree.  I just had no clue what your post meant.

And we'll document?  :)

geir


---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Richard Liang <ri...@gmail.com>.

Geir Magnusson Jr wrote:
> Richard Liang wrote:
>
>   
>> For this case, I decide to follow "useRadix(int radix)". Please correct
>> me if I'm wrong. Thanks a lot.
>>
>>     
>
> Do you mean that you're going to follow the spec?  If so, we should note
> that we're making a conscious decision to differ from the RI.
>
>   
Yes, Geir. For this case I think we have to differ from RI.
> geir
>
> ---------------------------------------------------------------------
> Terms of use : http://incubator.apache.org/harmony/mailing.html
> To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
> For additional commands, e-mail: harmony-dev-help@incubator.apache.org
>
>
>   

-- 
Richard Liang
China Software Development Lab, IBM 


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Geir Magnusson Jr <ge...@pobox.com>.

Richard Liang wrote:

> For this case, I decide to follow "useRadix(int radix)". Please correct
> me if I'm wrong. Thanks a lot.
> 

Do you mean that you're going to follow the spec?  If so, we should note
that we're making a conscious decision to differ from the RI.

geir

---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Richard Liang <ri...@gmail.com>.

Chris Gray wrote:
> On Thursday 06 July 2006 16:49, Geir Magnusson Jr wrote:
>   
>> This is a great example.  The last two aren't even legal exceptions for
>> that method.
>>
>> It seems like the RI is doing random crap, and it wouldn't be something
>> that someone would depend on... can you imagine?
>>     
>
> I think we have to distinguish between exceptions like these, which normally 
> nobody ever sees, unless they are actually writing tests for the core APIs 
> (or unless they make a major programming blunder - and then they'll fix that 
> and forget precisely what exception was thrown) on the one hand, and 
> exceptions which one can reasonably expect to happen sometimes when 
> developing code which may run in a variety of Java environments. An example 
> of the latter would be ClassNotFoundException indicating that the runtime 
> environment does not contain some wished-for class or package; if the 
> application programmer builds in a throw..catch clause which implements a 
> fallback, then you'd better theow ClassNotFoundException and not some random 
> thing like NoClassDefFoundError or NPE. Similarly, I just heard from a 
> customer that some application was failing because we were throwing 
> LinkageError when a shared library could not be found, whereas the 
> application only had a handler for UnsatisfiedLinkError. In this case both 
> the RI and the spec were in agreement, but I would happily have made the 
> change even if the spec had specified LinkageError and the RI was throwing 
> the subclass UnsatisfiedLinkError. 
>
> Fcourse it's not always easy to draw the line between exceptions which 
> probably represent a programmer error and those which robust programs may 
> need to handle, hance there will always be a need to discuss some of these 
> cases.
>
>   
Thanks a lot, Chris.

We may frequently encounter this confused situation, and I suggest we 
discuss the problems case by case if someone is not sure how to do. ;-)

For this case, I decide to follow "useRadix(int radix)". Please correct 
me if I'm wrong. Thanks a lot.

Best regards,
Richard
> Chris
> µ
>   

-- 
Richard Liang
China Software Development Lab, IBM 


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Chris Gray <ch...@kiffer.be>.
On Thursday 06 July 2006 16:49, Geir Magnusson Jr wrote:
> This is a great example.  The last two aren't even legal exceptions for
> that method.
>
> It seems like the RI is doing random crap, and it wouldn't be something
> that someone would depend on... can you imagine?

I think we have to distinguish between exceptions like these, which normally 
nobody ever sees, unless they are actually writing tests for the core APIs 
(or unless they make a major programming blunder - and then they'll fix that 
and forget precisely what exception was thrown) on the one hand, and 
exceptions which one can reasonably expect to happen sometimes when 
developing code which may run in a variety of Java environments. An example 
of the latter would be ClassNotFoundException indicating that the runtime 
environment does not contain some wished-for class or package; if the 
application programmer builds in a throw..catch clause which implements a 
fallback, then you'd better theow ClassNotFoundException and not some random 
thing like NoClassDefFoundError or NPE. Similarly, I just heard from a 
customer that some application was failing because we were throwing 
LinkageError when a shared library could not be found, whereas the 
application only had a handler for UnsatisfiedLinkError. In this case both 
the RI and the spec were in agreement, but I would happily have made the 
change even if the spec had specified LinkageError and the RI was throwing 
the subclass UnsatisfiedLinkError. 

Fcourse it's not always easy to draw the line between exceptions which 
probably represent a programmer error and those which robust programs may 
need to handle, hance there will always be a need to discuss some of these 
cases.

Chris
µ
-- 
Chris Gray        /k/ Embedded Java Solutions      BE0503765045
Embedded & Mobile Java, OSGi    http://www.k-embedded-java.com/
chris.gray@kiffer.be                             +32 3 216 0369


---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Geir Magnusson Jr <ge...@pobox.com>.

Richard Liang wrote:
> 
> 
> Geir Magnusson Jr wrote:
>> This is a great example.  The last two aren't even legal exceptions for
>> that method.
>>
>> It seems like the RI is doing random crap, and it wouldn't be something
>> that someone would depend on... can you imagine?
>>
>>   
> Thanks a lot, Geir. There may be a great deal of these "great examples".
> ;-)
> 
> This makes our discussion about "Exception throwing compatibility"  more
> complex.

No one said this was going to be easy :)

geir

---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Richard Liang <ri...@gmail.com>.

Geir Magnusson Jr wrote:
> This is a great example.  The last two aren't even legal exceptions for
> that method.
>
> It seems like the RI is doing random crap, and it wouldn't be something
> that someone would depend on... can you imagine?
>
>   
Thanks a lot, Geir. There may be a great deal of these "great examples". 
;-)

This makes our discussion about "Exception throwing compatibility"  more 
complex.

Best regards,
Richard.
> try {
>     Scanner.nextInt(foo);
> }
> catch {StringInxOutOfBndsEx bar) {
>     ... real problem...
> }
> catch (InputMismatchEx woogie) {
>     ... erm, a 1?
> }
> catch (PatternSyntaxException blough) {
>     .. a 0
> }
>
> This is something where I'd suggest we consider doing it per the spec,
> and noting the difference...  I can't even imagine someone depending on
> the IME or PSE to find 1 and 0 respectively...
>
> geir
>
>
> Richard Liang wrote:
>   
>> Hello All,
>>
>> When I'm trying to implement Scanner.nextInt(int radix), I met a problem.
>>
>> As we all know, Character.MIN_RADIX equals 2 and Character.MAX_RADIX
>> equals 36, so the parameter radix can not less than 2 or greater than
>> 36, Otherwise that parameter is illegal.
>>
>> But on RI, when the parameter radix is illegal, there are different
>> kinds of Exception thrown.
>>
>>    * If the parameter is less than 0 or greater than 36, RI throws a
>>      StringIndexOutOfBoundsException. Obviously this exception depends
>>      an RI's implementation.
>>    * If the parameter equals 1, RI throws an InputMismatchException
>>      with an additional information "The radix 1 is less than the
>>      Character.MIN_RADIX".
>>    * If the parameter equals 0, RI throws a
>>      java.util.regex.PatternSyntaxException whose constructor has three
>>      parameters. And the parameters depends on RI's implementation,
>>      that makes me can not follow RI.
>>
>> And nothing is documented in the spec. Shall I follow RI's behavior?
>> Thanks a lot.
>>
>> Here is the test case to demo this issue.
>>
>> public void test_nextIntI(){
>>       Scanner s = new Scanner("123 456");
>>
>>       try {
>>            s.nextInt(-1);
>>            fail("Should throw StringIndexOutOfBoundsException");
>>        } catch (StringIndexOutOfBoundsException e) {
>>            // Expected
>>        }
>>        try {
>>            s.nextInt(0);
>>            fail("Should throw PatternSyntaxException");
>>        } catch (PatternSyntaxException e) {
>>            // Expected
>>        }
>>        try {
>>            s.nextInt(1);
>>            fail("Should throw InputMismatchException");
>>        } catch (InputMismatchException e) {
>>            // Expected
>>        }
>>        try {
>>            s.nextInt(40);
>>            fail("Should throw StringIndexOutOfBoundsException");
>>        } catch (StringIndexOutOfBoundsException e) {
>>            // Expected
>>        }
>> }
>>
>>     
>
> ---------------------------------------------------------------------
> Terms of use : http://incubator.apache.org/harmony/mailing.html
> To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
> For additional commands, e-mail: harmony-dev-help@incubator.apache.org
>
>
>   

-- 
Richard Liang
China Software Development Lab, IBM 


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Geir Magnusson Jr <ge...@pobox.com>.
This is a great example.  The last two aren't even legal exceptions for
that method.

It seems like the RI is doing random crap, and it wouldn't be something
that someone would depend on... can you imagine?

try {
    Scanner.nextInt(foo);
}
catch {StringInxOutOfBndsEx bar) {
    ... real problem...
}
catch (InputMismatchEx woogie) {
    ... erm, a 1?
}
catch (PatternSyntaxException blough) {
    .. a 0
}

This is something where I'd suggest we consider doing it per the spec,
and noting the difference...  I can't even imagine someone depending on
the IME or PSE to find 1 and 0 respectively...

geir


Richard Liang wrote:
> Hello All,
> 
> When I'm trying to implement Scanner.nextInt(int radix), I met a problem.
> 
> As we all know, Character.MIN_RADIX equals 2 and Character.MAX_RADIX
> equals 36, so the parameter radix can not less than 2 or greater than
> 36, Otherwise that parameter is illegal.
> 
> But on RI, when the parameter radix is illegal, there are different
> kinds of Exception thrown.
> 
>    * If the parameter is less than 0 or greater than 36, RI throws a
>      StringIndexOutOfBoundsException. Obviously this exception depends
>      an RI's implementation.
>    * If the parameter equals 1, RI throws an InputMismatchException
>      with an additional information "The radix 1 is less than the
>      Character.MIN_RADIX".
>    * If the parameter equals 0, RI throws a
>      java.util.regex.PatternSyntaxException whose constructor has three
>      parameters. And the parameters depends on RI's implementation,
>      that makes me can not follow RI.
> 
> And nothing is documented in the spec. Shall I follow RI's behavior?
> Thanks a lot.
> 
> Here is the test case to demo this issue.
> 
> public void test_nextIntI(){
>       Scanner s = new Scanner("123 456");
> 
>       try {
>            s.nextInt(-1);
>            fail("Should throw StringIndexOutOfBoundsException");
>        } catch (StringIndexOutOfBoundsException e) {
>            // Expected
>        }
>        try {
>            s.nextInt(0);
>            fail("Should throw PatternSyntaxException");
>        } catch (PatternSyntaxException e) {
>            // Expected
>        }
>        try {
>            s.nextInt(1);
>            fail("Should throw InputMismatchException");
>        } catch (InputMismatchException e) {
>            // Expected
>        }
>        try {
>            s.nextInt(40);
>            fail("Should throw StringIndexOutOfBoundsException");
>        } catch (StringIndexOutOfBoundsException e) {
>            // Expected
>        }
> }
> 

---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Richard Liang <ri...@gmail.com>.

Anton Avtamonov wrote:
> Hi Richard,
>
> Very good example.
> You are right, spec says nothing about invalid radix processing for
> nextInt(). RI behavior just proves they have no guard check. However
> useRadix() produces IAE for the invalid radix and the sound of logic
> says that IAE is a proper reaction for wrong radix in any method with
> radix. Looks like a bug in RI for me...
>
> Don't beat me please :-), but I would suggest two approaches:
> - either no checks for radix, so that underlying algorithm does or
> doesn't produce some exceptions
> - implement guard check and produce IAE.
>
> I believe that right solution is the second one - produce IAE -
> however it potentailly results in less compatibility with RI.
>
Thanks a lot, Anton.

Actually, I'd like to follow RI's behavior except throwing a meaningless 
PatternSyntaxException when radix is 0.

It's said that RI will update spec instead of implementation when bug is 
reported[1] [2] ;-)

1. 
http://www.mail-archive.com/harmony-dev@incubator.apache.org/msg09588.html
2. 
http://www.mail-archive.com/harmony-dev@incubator.apache.org/msg09589.html

-- 
Richard Liang
China Software Development Lab, IBM 



---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Re: [classlib] Exception throwing compatibility: java.util.Scanner

Posted by Anton Avtamonov <an...@gmail.com>.
Hi Richard,

Very good example.
You are right, spec says nothing about invalid radix processing for
nextInt(). RI behavior just proves they have no guard check. However
useRadix() produces IAE for the invalid radix and the sound of logic
says that IAE is a proper reaction for wrong radix in any method with
radix. Looks like a bug in RI for me...

Don't beat me please :-), but I would suggest two approaches:
- either no checks for radix, so that underlying algorithm does or
doesn't produce some exceptions
- implement guard check and produce IAE.

I believe that right solution is the second one - produce IAE -
however it potentailly results in less compatibility with RI.

-- 
Anton Avtamonov,
Intel Middleware Products Division

On 7/6/06, Richard Liang <ri...@gmail.com> wrote:
> Hello All,
>
> When I'm trying to implement Scanner.nextInt(int radix), I met a problem.
>
> As we all know, Character.MIN_RADIX equals 2 and Character.MAX_RADIX
> equals 36, so the parameter radix can not less than 2 or greater than
> 36, Otherwise that parameter is illegal.
>
> But on RI, when the parameter radix is illegal, there are different
> kinds of Exception thrown.
>
>    * If the parameter is less than 0 or greater than 36, RI throws a
>      StringIndexOutOfBoundsException. Obviously this exception depends
>      an RI's implementation.
>    * If the parameter equals 1, RI throws an InputMismatchException
>      with an additional information "The radix 1 is less than the
>      Character.MIN_RADIX".
>    * If the parameter equals 0, RI throws a
>      java.util.regex.PatternSyntaxException whose constructor has three
>      parameters. And the parameters depends on RI's implementation,
>      that makes me can not follow RI.
>
> And nothing is documented in the spec. Shall I follow RI's behavior?
> Thanks a lot.
>
> Here is the test case to demo this issue.
>
> public void test_nextIntI(){
>       Scanner s = new Scanner("123 456");
>
>       try {
>            s.nextInt(-1);
>            fail("Should throw StringIndexOutOfBoundsException");
>        } catch (StringIndexOutOfBoundsException e) {
>            // Expected
>        }
>        try {
>            s.nextInt(0);
>            fail("Should throw PatternSyntaxException");
>        } catch (PatternSyntaxException e) {
>            // Expected
>        }
>        try {
>            s.nextInt(1);
>            fail("Should throw InputMismatchException");
>        } catch (InputMismatchException e) {
>            // Expected
>        }
>        try {
>            s.nextInt(40);
>            fail("Should throw StringIndexOutOfBoundsException");
>        } catch (StringIndexOutOfBoundsException e) {
>            // Expected
>        }
> }
>
> --
> Richard Liang
> China Software Development Lab, IBM
>
>
>

---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org