Java PatternSyntaxException Class getDescription() Method with Examples

Last Updated : 13 Jul, 2026

The getDescription() method of the PatternSyntaxException class returns a brief description of the syntax error that occurred while compiling a regular expression. It helps developers understand why a regex pattern is invalid.

  • Commonly used inside a catch (PatternSyntaxException e) block.
  • Returns only the error description, not the complete exception message.
Java
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

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

        try {
            // Invalid regular expression
            Pattern.compile("[A-Z");

        } catch (PatternSyntaxException e) {

            // Print the description of the syntax error
            System.out.println("Description: "
                    + e.getDescription());
        }
    }
}

Output
Description: Unclosed character class

Explanation: The regular expression "[A-Z" is invalid because the character class is not closed with a ]. When Pattern.compile() attempts to compile it, a PatternSyntaxException is thrown. The getDescription() method returns a brief description of the syntax error, which in this case is "Unclosed character class".

Syntax

public String getDescription()

  • Parameters: This method does not accept any parameters.
  • Return Value: Returns a String containing a short description of the regular expression syntax error.

Example 1: Java program to illustrate the working of getIndex() method

Java
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

class GFG {

    private static String regularExpression = "[";
    private static String input
        = "GeeksforGeeks "
          + "is a learning platform.";
    private static String replace = "Hi";

    public static void main(String[] args)
    {

        try {
            // Compile the pattern
            Pattern pattern
                = Pattern.compile(regularExpression);

            // To get a matcher object
            Matcher matcher = pattern.matcher(input);
            input = matcher.replaceAll(replace);
        }
        catch (PatternSyntaxException e) {

            // Print the description of the error occurred in
            // the pattern
            System.out.println("Description: "
                               + e.getDescription());
        }
    }
}

 
 


Output
Description: Unclosed character class

Explanation: The regular expression contains an opening square bracket ([) without a matching closing bracket (]). Therefore, Pattern.compile() throws a PatternSyntaxException, and the getDescription() method returns "Unclosed character class".

Example 2: Using getDescription() for an Illegal Repetition

Java
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

class GFG {

    // Declaring a string variable to store the regular
    // expression
    private static String regularExpression = "{";
    private static String input
        = "GeeksforGeeks "
          + "is a learning platform.";
    private static String replace = "Hello";

    public static void main(String[] args)
    {

        try {

            // Compile the pattern
            Pattern pattern
                = Pattern.compile(regularExpression);

            // To get a matcher object
            Matcher matcher = pattern.matcher(input);
            input = matcher.replaceAll(replace);
        }
        catch (PatternSyntaxException e) {

            // Print the index in the error of the pattern
            System.out.println("Description: "
                               + e.getDescription());
        }
    }
}

Output
Description: Illegal repetition

Explanation: The regular expression starts with the { quantifier without a preceding pattern to repeat. As a result, Pattern.compile() throws a PatternSyntaxException, and the getDescription() method returns "Illegal repetition".

Advantages of getDescription() 

  • Returns a concise explanation of the syntax error.
  • Simplifies debugging of regular expressions.
  • Helps identify invalid regex patterns quickly.
  • Improves readability of exception handling code.
Comment