The isSupported() method is a built-in method of the java.nio.charset checks if a given charset is supported or not.Â
Syntax:Â Â
public final boolean isSupported()
Parameters: The function accepts a single mandatory parameter charset Name which specifies the canonical name or the alias name which is to be checked.Â
Return Value: The function returns a boolean value. It returns true if it is supported, else it returns false.Â
Errors and Exceptions: The function throws two exceptions as shown below:Â Â
- IllegalCharsetNameException: It is thrown if the given charset name is illegal
- IllegalArgumentException : It is thrown if the given charset Name is null
Below is the implementation of the above function:
Program 1:Â Â
Java
// Java program to demonstrate// the above functionimport java.nio.charset.Charset;Â
public class GFG {Â
    public static void main(String[] args)    {        try {            System.out.println("ISO-2022-CN"                               + " is supported or not? :"                               + Charset.isSupported("ISO-2022-CN"));        }        catch (Exception e) {            System.out.println("Exception: "                               + e);        }    }} |
ISO-2022-CN is supported or not? :true
Â
Program 2:Â
Java
// Java program to demonstrate// the above functionimport java.nio.charset.Charset;Â
public class GFG {Â
    public static void main(String[] args)    {        try {            System.out.println("ISO is "                               + "supported or not? :"                               + Charset.isSupported("ISO"));        }        catch (Exception e) {            System.out.println("Exception: " + e);        }    }} |
ISO is supported or not? :false
Â
Program 3:Â
Java
// Java program to demonstrate// the above functionimport java.nio.charset.Charset;Â
public class GFG {Â
    public static void main(String[] args)    {        try {            System.out.println("NULL is "                               + "supported or not? :"                               + Charset.isSupported(""));        }        catch (Exception e) {            System.out.println("Exception: "                               + e);        }    }} |
Exception is java.nio.charset.IllegalCharsetNameException:
Â
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/charset/Charset.html#isSupported-java.lang.String-
Â
