Java : UnsupportedEncodingException - API使用例
UnsupportedEncodingException (Java SE 23 & JDK 23) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
APIドキュメントのおともにどうぞ。
概要
文字のエンコーディングがサポートされていません。
UnsupportedEncodingException はチェック例外です。
サポートされていない文字コードを使おうとすると発生します。
try {
final var ret = "abc".getBytes("Shift_JIS");
System.out.println(Arrays.toString(ret));
} catch (UnsupportedEncodingException e) {
System.out.println("UnsupportedEncodingException!");
}
// 結果
// ↓
//[97, 98, 99]
try {
final var ret = "abc".getBytes("XXX");
System.out.println(Arrays.toString(ret));
} catch (UnsupportedEncodingException e) {
System.out.println("UnsupportedEncodingException!");
}
// 結果
// ↓
//UnsupportedEncodingException!
コンストラクタ
UnsupportedEncodingException ()
詳細メッセージを持たないUnsupportedEncodingExceptionを構築します。
final var e = new UnsupportedEncodingException();
System.out.println(e); // java.io.UnsupportedEncodingException
UnsupportedEncodingException (String s)
詳細メッセージを持つUnsupportedEncodingExceptionを構築します。
final var e = new UnsupportedEncodingException("abc");
System.out.println(e); // java.io.UnsupportedEncodingException: abc
System.out.println(e.getMessage()); // abc
Throwableで宣言されたメソッド
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
「Java API 使用例 : Throwable」をご参照ください。
関連記事
- API 使用例
- Error (エラー)
- Exception (チェック例外)
- RuntimeException (非チェック例外)
- ArithmeticException (算術例外)
- ArrayIndexOutOfBoundsException
- ArrayStoreException
- BufferOverflowException
- BufferUnderflowException
- CancellationException
- ClassCastException (キャスト例外)
- ConcurrentModificationException (並列処理例外)
- DateTimeException (日付・時刻の例外)
- DateTimeParseException (日付・時刻の解析例外)
- IllegalArgumentException
- IllegalStateException
- IndexOutOfBoundsException
- NoSuchElementException
- NullPointerException
- PatternSyntaxException
- ReadOnlyBufferException
- StringIndexOutOfBoundsException
- UnsupportedOperationException
- Throwable