public static class RegexUtil.CheckedPatternSyntaxException
extends java.lang.Exception
PatternSyntaxException
.
This exception is useful when an illegal regex is detected but the contextual information to report a helpful error message is not available at the current depth in the call stack. By using a checked PatternSyntaxException the error must be handled up the call stack where a better error message can be reported.
Typical usage is:
void myMethod(...) throws CheckedPatternSyntaxException { ... if (! isRegex(myString)) { throw new CheckedPatternSyntaxException(...); } ... Pattern.compile(myString) ...Simply calling
Pattern.compile
would have a similar effect, in that PatternSyntaxException
would be thrown at run time if myString
is not a regular
expression. There are two problems with such an approach. First, a client of myMethod
might forget to handle the exception, since PatternSyntaxException
is not checked.
Also, the Regex Checker would issue a warning about the call to Pattern.compile
that
might throw an exception. The above usage pattern avoids both problems.PatternSyntaxException
,
Serialized FormConstructor and Description |
---|
CheckedPatternSyntaxException(java.util.regex.PatternSyntaxException pse)
Constructs a new CheckedPatternSyntaxException equivalent to the given
PatternSyntaxException . |
CheckedPatternSyntaxException(java.lang.String desc,
java.lang.String regex,
int index)
Constructs a new CheckedPatternSyntaxException.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getDescription()
Retrieves the description of the error.
|
int |
getIndex()
Retrieves the error index.
|
java.lang.String |
getMessage()
Returns a multi-line string containing the description of the syntax error and its index,
the erroneous regular-expression pattern, and a visual indication of the error index
within the pattern.
|
java.lang.String |
getPattern()
Retrieves the erroneous regular-expression pattern.
|
public CheckedPatternSyntaxException(java.util.regex.PatternSyntaxException pse)
PatternSyntaxException
.
Consider calling this constructor with the result of RegexUtil.regexError(java.lang.String)
.
pse
- the PatternSyntaxException to be wrappedpublic CheckedPatternSyntaxException(java.lang.String desc, java.lang.String regex, int index)
desc
- a description of the errorregex
- the erroneous patternindex
- the approximate index in the pattern of the error, or -1 if the index is not
knownpublic java.lang.String getDescription()
public int getIndex()
-1
if the index is
not known@Pure public java.lang.String getMessage()
getMessage
in class java.lang.Throwable
public java.lang.String getPattern()