You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directory.apache.org by Felix Knecht <fe...@otego.com> on 2010/03/06 14:10:11 UTC
Swallowing Exceptions
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
IMO it's ok to return a default value in case of exception, but it's
hard to find the error if you can't find anything in the log. ( in case
of encoding exception)
Is this so on purpose or is the error logged anywhere else (not obvious
to me)?
Felix
shared-ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
[3144 ff]
public static final String utf8ToString( byte[] bytes, int start,
int length )
{
if ( bytes == null )
{
return "";
}
try
{
return new String( bytes, start, length, "UTF-8" );
}
catch ( UnsupportedEncodingException uee )
{
return "";
}
}
/**
* Return UTF-8 encoded byte[] representation of a String
*
* @param string The string to be transformed to a byte array
* @return The transformed byte array
*/
public static final byte[] getBytesUtf8( String string )
{
if ( string == null )
{
return new byte[0];
}
try
{
return string.getBytes( "UTF-8" );
}
catch ( UnsupportedEncodingException uee )
{
return new byte[]
{};
}
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAkuSVDMACgkQ2lZVCB08qHG6KQCgsbKUAX/ny4sK6Vht0fusXd4H
dh4AoOq1KOKM6siPVeP4oakmSUqAyBVP
=MUhh
-----END PGP SIGNATURE-----
Re: Swallowing Exceptions
Posted by Stefan Seelmann <se...@apache.org>.
Done.
Emmanuel Lecharny wrote:
> This method is a bit special. The exception swallowing was done in
> purpose, as it's guaranteed that the conversion will not fail.
>
> However, I agree with Stefan : it would probably a better idea to thow a
> RuntimeException in this case. Feel free to modify the code accordingly.
>
> On Sat, Mar 6, 2010 at 2:25 PM, Stefan Seelmann <seelmann@apache.org
> <ma...@apache.org>> wrote:
>
> Felix Knecht wrote:
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> IMO it's ok to return a default value in case of exception, but it's
> hard to find the error if you can't find anything in the log. (
> in case
> of encoding exception)
>
> Is this so on purpose or is the error logged anywhere else (not
> obvious
> to me)?
>
>
> I think it that case we could wrap the UnsupportedEncodingException
> into a RuntimeException. We know that UTF-8 is always supported, and
> in case it is not there then something really strange is going on.
>
> My 2 cents.
> Stefan
>
>
>
> Felix
>
>
> shared-ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
> [3144 ff]
>
> public static final String utf8ToString( byte[] bytes, int start,
> int length )
> {
> if ( bytes == null )
> {
> return "";
> }
>
> try
> {
> return new String( bytes, start, length, "UTF-8" );
> }
> catch ( UnsupportedEncodingException uee )
> {
> return "";
> }
> }
>
>
> /**
> * Return UTF-8 encoded byte[] representation of a String
> *
> * @param string The string to be transformed to a byte array
> * @return The transformed byte array
> */
> public static final byte[] getBytesUtf8( String string )
> {
> if ( string == null )
> {
> return new byte[0];
> }
>
> try
> {
> return string.getBytes( "UTF-8" );
> }
> catch ( UnsupportedEncodingException uee )
> {
> return new byte[]
> {};
> }
> }
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2.0.14 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkuSVDMACgkQ2lZVCB08qHG6KQCgsbKUAX/ny4sK6Vht0fusXd4H
> dh4AoOq1KOKM6siPVeP4oakmSUqAyBVP
> =MUhh
> -----END PGP SIGNATURE-----
>
>
>
>
>
> --
> Regards,
> Cordialement,
> Emmanuel Lécharny
> www.iktek.com <http://www.iktek.com>
Re: Swallowing Exceptions
Posted by Emmanuel Lecharny <el...@apache.org>.
This method is a bit special. The exception swallowing was done in purpose,
as it's guaranteed that the conversion will not fail.
However, I agree with Stefan : it would probably a better idea to thow a
RuntimeException in this case. Feel free to modify the code accordingly.
On Sat, Mar 6, 2010 at 2:25 PM, Stefan Seelmann <se...@apache.org> wrote:
> Felix Knecht wrote:
>
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> IMO it's ok to return a default value in case of exception, but it's
>> hard to find the error if you can't find anything in the log. ( in case
>> of encoding exception)
>>
>> Is this so on purpose or is the error logged anywhere else (not obvious
>> to me)?
>>
>
> I think it that case we could wrap the UnsupportedEncodingException into a
> RuntimeException. We know that UTF-8 is always supported, and in case it is
> not there then something really strange is going on.
>
> My 2 cents.
> Stefan
>
>
>
>> Felix
>>
>>
>>
>> shared-ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
>> [3144 ff]
>>
>> public static final String utf8ToString( byte[] bytes, int start,
>> int length )
>> {
>> if ( bytes == null )
>> {
>> return "";
>> }
>>
>> try
>> {
>> return new String( bytes, start, length, "UTF-8" );
>> }
>> catch ( UnsupportedEncodingException uee )
>> {
>> return "";
>> }
>> }
>>
>>
>> /**
>> * Return UTF-8 encoded byte[] representation of a String
>> *
>> * @param string The string to be transformed to a byte array
>> * @return The transformed byte array
>> */
>> public static final byte[] getBytesUtf8( String string )
>> {
>> if ( string == null )
>> {
>> return new byte[0];
>> }
>>
>> try
>> {
>> return string.getBytes( "UTF-8" );
>> }
>> catch ( UnsupportedEncodingException uee )
>> {
>> return new byte[]
>> {};
>> }
>> }
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v2.0.14 (GNU/Linux)
>> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>>
>> iEYEARECAAYFAkuSVDMACgkQ2lZVCB08qHG6KQCgsbKUAX/ny4sK6Vht0fusXd4H
>> dh4AoOq1KOKM6siPVeP4oakmSUqAyBVP
>> =MUhh
>> -----END PGP SIGNATURE-----
>>
>
>
--
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com
Re: Swallowing Exceptions
Posted by Stefan Seelmann <se...@apache.org>.
Felix Knecht wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> IMO it's ok to return a default value in case of exception, but it's
> hard to find the error if you can't find anything in the log. ( in case
> of encoding exception)
>
> Is this so on purpose or is the error logged anywhere else (not obvious
> to me)?
I think it that case we could wrap the UnsupportedEncodingException into
a RuntimeException. We know that UTF-8 is always supported, and in case
it is not there then something really strange is going on.
My 2 cents.
Stefan
>
> Felix
>
>
> shared-ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
> [3144 ff]
>
> public static final String utf8ToString( byte[] bytes, int start,
> int length )
> {
> if ( bytes == null )
> {
> return "";
> }
>
> try
> {
> return new String( bytes, start, length, "UTF-8" );
> }
> catch ( UnsupportedEncodingException uee )
> {
> return "";
> }
> }
>
>
> /**
> * Return UTF-8 encoded byte[] representation of a String
> *
> * @param string The string to be transformed to a byte array
> * @return The transformed byte array
> */
> public static final byte[] getBytesUtf8( String string )
> {
> if ( string == null )
> {
> return new byte[0];
> }
>
> try
> {
> return string.getBytes( "UTF-8" );
> }
> catch ( UnsupportedEncodingException uee )
> {
> return new byte[]
> {};
> }
> }
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2.0.14 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkuSVDMACgkQ2lZVCB08qHG6KQCgsbKUAX/ny4sK6Vht0fusXd4H
> dh4AoOq1KOKM6siPVeP4oakmSUqAyBVP
> =MUhh
> -----END PGP SIGNATURE-----