Pattern Syntax Exception Class in Java
The `PatternSyntaxException` class in Java is a crucial component of regular expression handling, detecting syntax errors during regex compilation. It halts the process and provides valuable information, enabling efficient debugging and iteration. It inherits from Java's `java.lang.Exception` superclass and offers accessor methods for retrieving error details. It contributes to the robustness of regex-based applications.
Syntax
public class PatternSyntaxException extends IllegalArgumentException implements Serializable
Methods in PatternSyntaxException Class
The PatternSyntaxException class in Java provides several methods for retrieving information about the syntax error encountered when compiling a regular expression pattern.
1. int getIndex(): Returns the overall index in the regular expression pattern where the syntax error occurred.
2. int getErrorIndex(): Returns the specific index within the regular expression pattern where the syntax error was detected.
3. String getDescription(): Returns a descriptive message explaining the syntax error encountered.
4. String getPattern(): Returns the erroneous regular expression that caused the syntax error.
5. Throwable getCause(): Returns the cause of the following exception or null if the cause is nonexistent or unknown.
Here is an implementation of the methods mentioned above:
FileName:PatternSyntaxExceptionImp.java
import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; public class PatternSyntaxExceptionImp { public static void main(String[] args) { String regex = "[a-z"; // Erroneous regular expression with missing closing bracket try { Pattern pattern = Pattern.compile(regex); // If compilation is successful, continue with further processing } catch (PatternSyntaxException e) { // Handle the syntax error System.out.println("Syntax error description: " + e.getDescription()); System.out.println("Overall index of syntax error: " + e.getIndex()); System.out.println("Erroneous regular expression: " + e.getPattern()); System.out.println("Cause of the exception: " + e.getCause()); } } }
Output:
Syntax error description: Unclosed character class Overall index of syntax error: 3 Erroneous regular expression: [a-z Cause of the exception: null
Inherited Methods From java.lang.Throwable Class
The `PatternSyntaxException` class inherits several methods from its superclass, `java.lang.Throwable`.
Here are the inherited methods:
1. String getMessage(): Returns a detailed message about the exception.
2. String getLocalizedMessage(): Returns a localized message appropriate for the default locale.
3. void printStackTrace(): Prints the stack trace of the exception to the standard error stream.
4. void printStackTrace(PrintStream s): Prints the stack trace of the exception to the specified print stream.
5. void printStackTrace(PrintWriter s): Prints the stack trace of the exception to the specified print writer.
6. Throwable getCause(): Returns the cause of the following exception or null if the cause is nonexistent or unknown.
7. String toString(): Returns a short description of the exception, including the exception's class name and message.
8. void addSuppressed(Throwable exception): Appends the specified exception to the exceptions that were suppressed in order to deliver the following exception.
Here is an implementation of the methods mentioned above:
FileName: PatternSyntaxExceptionImp1 .java
import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; public class PatternSyntaxExceptionImp1 { public static void main(String[] args) { String regex = "[a-z"; // Erroneous regular expression with missing closing bracket try { Pattern pattern = Pattern.compile(regex); // If compilation is successful, continue with further processing } catch (PatternSyntaxException e) { // Handle the syntax error // getMessage() method System.out.println("Exception message: " + e.getMessage()); // getLocalizedMessage() method System.out.println("Localized message: " + e.getLocalizedMessage()); // printStackTrace() method System.out.println("Stack Trace:"); e.printStackTrace(); // getCause() method System.out.println("Cause of the exception: " + e.getCause()); // toString() method System.out.println("Exception details: " + e.toString()); // addSuppressed() method try { throw new NullPointerException("Simulated cause"); } catch (NullPointerException ex) { e.addSuppressed(ex); } } } }
Output:
Exception message: Unclosed character class near index 3 [a-z ^ Localized message: Unclosed character class near index 3 [a-z ^ Stack Trace: Cause of the exception: null Exception details: java.util.regex.PatternSyntaxException: Unclosed character class near index 3 [a-z ^ java.util.regex.PatternSyntaxException: Unclosed character class near index 3 [a-z ^ at java.base/java.util.regex.Pattern.error(Pattern.java:2028) at java.base/java.util.regex.Pattern.clazz(Pattern.java:2690) at java.base/java.util.regex.Pattern.sequence(Pattern.java:2139) at java.base/java.util.regex.Pattern.expr(Pattern.java:2069) at java.base/java.util.regex.Pattern.compile(Pattern.java:1783) at java.base/java.util.regex.Pattern.(Pattern.java:1430) at java.base/java.util.regex.Pattern.compile(Pattern.java:1069) at PatternSyntaxExceptionImp1.main(PatternSyntaxExceptionImp1.java:9) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at jdk.compiler/com.sun.tools.javac.launcher.Main.execute(Main.java:419) at jdk.compiler/com.sun.tools.javac.launcher.Main.run(Main.java:192) at jdk.compiler/com.sun.tools.javac.launcher.Main.main(Main.java:132)
Inherited Methods From java.lang.Object Class
The `PatternSyntaxException` class inherits several methods from the `java.lang.Object` class. Here are the inherited methods:
1. boolean equals(Object obj): Indicates whether some other object is "equal to" the following one.
2. protected void finalize() throws Throwable: Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
3. Class getClass(): Returns the runtime class of an object.
4. int hashCode(): Returns a hash code value for the object.
5. void notify(): Wakes up a single thread that is waiting on the following object's monitor.
6. void notifyAll(): Wakes up all threads that are waiting on the following object's monitor.
7. String toString(): Returns a string representation of the object.
8. void wait() throws InterruptedException: Causes the current thread to wait until another thread invokes the `notify()` method or the `notifyAll()` method for the object.
9. void wait(long timeout) throws InterruptedException: Causes the current thread to wait until either another thread invokes the `notify()` method or the `notifyAll()` method for the following object or a specified amount of time has elapsed.
10. void wait(long timeout, int nanos) throws InterruptedException: Causes the current thread to wait until another thread invokes the `notify()` method or the `notifyAll()` method for the following object, or some other thread interrupts the current thread or a certain amount of real-time has elapsed.
Here is an implementation of the methods mentioned above:
FileName:PatternSyntaxExceptionImp2.java
import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; public class PatternSyntaxExceptionImp2 { public static void main(String[] args) { String regex = "[a-z"; // Erroneous regular expression with missing closing bracket try { Pattern pattern = Pattern.compile(regex); // If compilation is successful, continue with further processing } catch (PatternSyntaxException e) { // Handle the syntax error // toString() method System.out.println("PatternSyntaxException details: " + e.toString()); // hashCode() method System.out.println("Hash code of the exception: " + e.hashCode()); // getClass() method System.out.println("Class of the exception: " + e.getClass()); // equals() method PatternSyntaxException otherException = new PatternSyntaxException("Other syntax error", regex, 0); System.out.println("Are the exceptions equal? " + e.equals(otherException)); } } }
Output:
PatternSyntaxException details: java.util.regex.PatternSyntaxException: Unclosed character class near index 3 [a-z ^ Hash code of the exception: 1816089958 Class of the exception: class java.util.regex.PatternSyntaxException Are the exceptions equal? false