Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > UnsupportedEncodingException

Reply
Thread Tools

UnsupportedEncodingException

 
 
Bill Lattery via JavaKB.com
Guest
Posts: n/a
 
      11-19-2004

I am using java 1.4.2 with Sun One Studio 5.

I am using a getBytes() function:

String mac = "1.2.3.4"
byte[] bv = mac.getBytes("UTF-16");

When I try to compile I get this message:

"unreported exception java.io.UnsupportedEncodingException; must be caught or declared to be thrown"

Why do I get this?

Please help.

Thanks.

_____________________
Message posted via http://www.javakb.com
 
Reply With Quote
 
 
 
 
Ann
Guest
Posts: n/a
 
      11-19-2004

"Bill Lattery via JavaKB.com" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed).. .
>
> I am using java 1.4.2 with Sun One Studio 5.
>
> I am using a getBytes() function:
>
> String mac = "1.2.3.4"
> byte[] bv = mac.getBytes("UTF-16");
>
> When I try to compile I get this message:
>
> "unreported exception java.io.UnsupportedEncodingException; must be caught

or declared to be thrown"
>
> Why do I get this?


You need a semicolon on the first line.


 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      11-20-2004
On Fri, 19 Nov 2004 22:38:44 GMT, Bill Lattery via JavaKB.com wrote:

// as Ann mentioned, this statement will not compile..
// please copy/paste code.

> String mac = "1.2.3.4"


String mac = "1.2.3.4";
try {

> byte[] bv = mac.getBytes("UTF-16");


} catch(UnsupportedEncodingException uee) {
uee.printStackTrace();
}

> Please help.


Note that a better groups for people starting in Java is..
<http://www.physci.org/codes/javafaq.jsp#cljh>

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      11-20-2004
"Bill Lattery via JavaKB.com" <(E-Mail Removed)> writes:

> When I try to compile I get this message:
>
> "unreported exception java.io.UnsupportedEncodingException; must be
> caught or declared to be thrown"
>
> Why do I get this?


Because you call a method that is DOCUMENTED to throw an unchecked
exception by that name. You either need to declare your method to
throw it, or surround the call to getBytes() with try/catch.
 
Reply With Quote
 
Boudewijn Dijkstra
Guest
Posts: n/a
 
      11-20-2004
"Andrew Thompson" <(E-Mail Removed)> schreef in bericht
news:12rhz10v5b5bo$(E-Mail Removed)...
> On Fri, 19 Nov 2004 22:38:44 GMT, Bill Lattery via JavaKB.com wrote:
>
> // as Ann mentioned, this statement will not compile..
> // please copy/paste code.
>
>> String mac = "1.2.3.4"

>
> String mac = "1.2.3.4";
> try {
>
>> byte[] bv = mac.getBytes("UTF-16");

>
> } catch(UnsupportedEncodingException uee) {
> uee.printStackTrace();
> }


I think it would be better to
throw new InternalError("missing required encoding: UTF-16");
in the catch block.


 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      11-22-2004
Tor Iver Wilhelmsen wrote:

> "Bill Lattery via JavaKB.com" <(E-Mail Removed)> writes:
>
>
>>When I try to compile I get this message:
>>
>>"unreported exception java.io.UnsupportedEncodingException; must be
>>caught or declared to be thrown"
>>
>>Why do I get this?

>
>
> Because you call a method that is DOCUMENTED to throw an unchecked
> exception by that name. You either need to declare your method to
> throw it, or surround the call to getBytes() with try/catch.


Documentation has little to do with it. UnsupportedEncodingException is
a *checked* exception that the method being invoked is declared to be
capable of throwing, therefore the method that invokes it must either
catch that exception or declare that it throws it. This is _never_ the
case for unchecked exceptions, whether they are declared in a throws
clause or not.


John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Bill Lattery via JavaKB.com
Guest
Posts: n/a
 
      11-22-2004
Thanks Andrew!

You method works.

Now, I have another problem.

I am working with a Java-based program that performs SNMP. One of the MIB variables is a 6 byte octet message stream. The program retrieves the information and saves it on a table as a String. My part of the code must get the info as a String. I need to convert the String to a byte buffer.

The problem I have is that not all of the data is encoded properly.

For example: The MIB variable message is (in decimal): 00 04 -127 12 -115 -114

When I use the getBytes() function, the byte buffer contains (in decimal): 00 04 63 12 63 63.

I have used the Character Encoding Sets US-ASCII, ISO-8859-1, UTF-8, UTF-16, UTF-16BE, UTF-16LE, and windows-1252. None have given me the result I need.

What Character Encoding Set should I use to get the right values?

Thanks.

--
Message posted via http://www.javakb.com
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      11-22-2004
On Mon, 22 Nov 2004 15:45:45 GMT, Bill Lattery via JavaKB.com wrote:

> Thanks Andrew!
>
> You method works.


You're welcome.

> Now, I have another problem.

....
> The problem I have is that not all of the data is encoded properly.


Unfortunately encodings are a bit out of my field of expertise.
I'll wait to hear what the experts say.

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      11-22-2004
"John C. Bollinger" <(E-Mail Removed)> writes:

> Documentation has little to do with it. UnsupportedEncodingException
> is a *checked* exception that the method being invoked is declared to
> be capable of throwing, therefore the method that invokes it must
> either catch that exception or declare that it throws it.


Sorry, yes, typed a bit fast there: I meant to write checked.
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      11-22-2004
Bill Lattery via JavaKB.com wrote:

> I am working with a Java-based program that performs SNMP. One of the MIB variables is a 6 byte octet message stream. The program retrieves the information and saves it on a table as a String. My part of the code must get the info as a String. I need to convert the String to a byte buffer.


The MIB / SNMP terminology appears to be "octet _string_" (emphasis
mine) not "octet message stream".

> The problem I have is that not all of the data is encoded properly.


That's rather unlikely. It is entirely possible that you don't know how
it is encoded, however.

> For example: The MIB variable message is (in decimal): 00 04 -127 12 -115 -114
>
> When I use the getBytes() function, the byte buffer contains (in decimal): 00 04 63 12 63 63.


To do this you have already coerced the byte sequence into a String
according to some encoding. The byte 63 (decimal) is a '?' in Unicode;
it signals that the charset you used does not map the current byte(s) to
a character.

> I have used the Character Encoding Sets US-ASCII, ISO-8859-1, UTF-8, UTF-16, UTF-16BE, UTF-16LE, and windows-1252. None have given me the result I need.


How would you recognize the result when you got it? If you know what
the correct answer is, then that would be very pertinent.

> What Character Encoding Set should I use to get the right values?


That may be device-dependent. MIB specifies data types in ASN.1, and
ASN.1 apparently does not define encodings to use (that's part of the
"Abstract" in "Abstract Syntax Notation"). The information is probably
carried by some other item in the MIB.

Have you considered the possibility that the string is empty? The
initial zero byte is very suspicious in that regard. If you are getting
fixed-length strings with variable-length content then there will be
some kind of fence byte to indicate the end of the meaningful data -- at
least when the string is shorter than the maximum length. Compare to C
strings.

If that's not it then you'll probably need to pass on more information.
For what it's worth, the charsets you have already tried cover all the
likely ones.


John Bollinger
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UnsupportedEncodingException Stefan Poehn Java 0 07-26-2004 07:42 AM
Re: UnsupportedEncodingException Jon A. Cruz Java 0 07-25-2003 03:43 AM



Advertisments