Charset.isSupported(): This method is available in java.nio.charset.Charset class of Java.
Syntax:
boolean java.nio.charset.Charset.isSupported(String charsetName)
This method takes one argument of type String as its parameter. This method tells whether the named charset is supported.
Parameters: One parameter is required for this method.
charsetName: The name of the requested charset; may be either a canonical name or an alias.
Returns: true if, and only if, support for the named charset is available in the current Java virtual machine.
Throws:
1. IllegalCharsetNameException - If the given charset name is illegal.
2. IllegalArgumentException - If the given charsetName is null.
For Example:
String charsetName = "UTF-8"
Charset.isSupported(charsetName) = > It returns true.
Approach 1: When no exceptions.
Java
import java.nio.charset.Charset;public class CharsetisSupported {public static void main(String[] args) {String charsetName = "UTF-8";System.out.println(Charset.isSupported(charsetName));}}
Output:
true
Approach 2: IllegalCharsetNameException
Java
import java.nio.charset.Charset;public class CharsetisSupported {public static void main(String[] args) {String charsetName = "";System.out.println(Charset.isSupported(charsetName));}}
Output:
Exception in thread "main" java.nio.charset.IllegalCharsetNameException: at java.base/java.nio.charset.Charset.checkName(Charset.java:293) at java.base/java.nio.charset.Charset.lookup2(Charset.java:479) at java.base/java.nio.charset.Charset.lookup(Charset.java:459) at java.base/java.nio.charset.Charset.isSupported(Charset.java:500)
Approach 3: IllegalArgumentException
Java
import java.nio.charset.Charset;public class CharsetisSupported {public static void main(String[] args) {String charsetName = null;System.out.println(Charset.isSupported(charsetName));}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: Null charset name at java.base/java.nio.charset.Charset.lookup(Charset.java:452) at java.base/java.nio.charset.Charset.isSupported(Charset.java:500)
No comments:
Post a Comment