Character.UnicodeBlock.forName() in Java

Character.UnicodeBlock.forName() : This method is availabe in java.lang.Character.UnicodeBlock.

Syntax:

UnicodeBlock java.lang.Character.UnicodeBlock.forName(String blockName)

This method takes one argument of type String as its parameter. This method returns the UnicodeBlock with the given name. Block names are determined by The Unicode Standard.

Parameters: One parameter is required for this method.

blockName: A UnicodeBlock name.

Returns: The UnicodeBlock instance identified by blockName.

Throws:

1. IllegalArgumentException - if blockName is an invalid name.

2. NullPointerException - if blockName is null.

Approach 1: When no exceptions.

Java

public class CharacterUnicodeBlockforName {
    public static void main(String[] args) {

        String blockName = "ZANABAZAR_SQUARE";
        System.out.println(Character.UnicodeBlock.forName(blockName));

    }
}

Output:

ZANABAZAR_SQUARE


Approach 2: IllegalArgumentException 

Java

public class CharacterUnicodeBlockforName {
    public static void main(String[] args) {

        String blockName = "Hello";
        System.out.println(Character.UnicodeBlock.forName(blockName));

    }
}


Output:

Exception in thread "main" java.lang.IllegalArgumentException: Not a valid block name: Hello at java.base/java.lang.Character$UnicodeBlock.forName(Character.java:4212)



Approach 3: NullPointerException 

Java

public class CharacterUnicodeBlockforName {
    public static void main(String[] args) {

        String blockName = null;
        System.out.println(Character.UnicodeBlock.forName(blockName));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toUpperCase(java.util.Locale)" because "blockName" is null at java.base/java.lang.Character$UnicodeBlock.forName(Character.java:4210)


No comments:

Post a Comment