UnsupportedEncodingException

Discussion in 'Java' started by Bill Lattery via JavaKB.com, Nov 19, 2004.

  1. 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
     
    Bill Lattery via JavaKB.com, Nov 19, 2004
    #1
    1. Advertising

  2. Bill Lattery via JavaKB.com

    Ann Guest

    "Bill Lattery via JavaKB.com" <> wrote in message
    news:...
    >
    > 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.
     
    Ann, Nov 19, 2004
    #2
    1. Advertising

  3. 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
     
    Andrew Thompson, Nov 20, 2004
    #3
  4. "Bill Lattery via JavaKB.com" <> 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.
     
    Tor Iver Wilhelmsen, Nov 20, 2004
    #4
  5. "Andrew Thompson" <> schreef in bericht
    news:12rhz10v5b5bo$...
    > 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.
     
    Boudewijn Dijkstra, Nov 20, 2004
    #5
  6. Tor Iver Wilhelmsen wrote:

    > "Bill Lattery via JavaKB.com" <> 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
     
    John C. Bollinger, Nov 22, 2004
    #6
  7. 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
     
    Bill Lattery via JavaKB.com, Nov 22, 2004
    #7
  8. 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
     
    Andrew Thompson, Nov 22, 2004
    #8
  9. "John C. Bollinger" <> 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.
     
    Tor Iver Wilhelmsen, Nov 22, 2004
    #9
  10. 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
     
    John C. Bollinger, Nov 22, 2004
    #10
  11. "Bill Lattery via JavaKB.com" <> schreef in bericht
    news:...
    > 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


    If you interpret these as characters (which they are obviously *not*), you
    get:
    0000 <control> NULL
    0004 <control> END OF TEXT
    FF81 HALFWIDTH KATAKANA LETTER TI
    000C <control> FORM FEED (FF)
    FF8D HALFWIDTH KATAKANA LETTER HE
    FF8E HALFWIDTH KATAKANA LETTER HO

    Don't expect any encoding to live up to this. Either create the byte array
    yourself, or do &0xFF on every char and then use getBytes with ISO-8859-1.
     
    Boudewijn Dijkstra, Nov 23, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon A. Cruz

    Re: UnsupportedEncodingException

    Jon A. Cruz, Jul 25, 2003, in forum: Java
    Replies:
    0
    Views:
    1,292
    Jon A. Cruz
    Jul 25, 2003
  2. Stefan Poehn

    UnsupportedEncodingException

    Stefan Poehn, Jul 26, 2004, in forum: Java
    Replies:
    0
    Views:
    520
    Stefan Poehn
    Jul 26, 2004
Loading...

Share This Page