1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 * 17 */ 18 19 package java.util.regex; 20 21 /** 22 * Unchecked exception thrown to indicate a syntax error in a 23 * regular-expression pattern. 24 * 25 * @author unascribed 26 * @since 1.4 27 * @spec JSR-51 28 */ 29 30 public class PatternSyntaxException 31 extends IllegalArgumentException 32 { 33 private static final long serialVersionUID = -3864639126226059218L; 34 35 private final String desc; 36 private final String pattern; 37 private final int index; 38 39 /** 40 * Constructs a new instance of this class. 41 * 42 * @param desc 43 * A description of the error 44 * 45 * @param regex 46 * The erroneous pattern 47 * 48 * @param index 49 * The approximate index in the pattern of the error, 50 * or {@code -1} if the index is not known 51 */ 52 public PatternSyntaxException(String desc, String regex, int index) { 53 this.desc = desc; 54 this.pattern = regex; 55 this.index = index; 56 } 57 58 /** 59 * Retrieves the error index. 60 * 61 * @return The approximate index in the pattern of the error, 62 * or {@code -1} if the index is not known 63 */ 64 public int getIndex() { 65 return index; 66 } 67 68 /** 69 * Retrieves the description of the error. 70 * 71 * @return The description of the error 72 */ 73 public String getDescription() { 74 return desc; 75 } 76 77 /** 78 * Retrieves the erroneous regular-expression pattern. 79 * 80 * @return The erroneous pattern 81 */ 82 public String getPattern() { 83 return pattern; 84 } 85 86 /** 87 * Returns a multi-line string containing the description of the syntax 88 * error and its index, the erroneous regular-expression pattern, and a 89 * visual indication of the error index within the pattern. 90 * 91 * @return The full detail message 92 */ 93 public String getMessage() { 94 StringBuilder sb = new StringBuilder(); 95 sb.append(desc); 96 if (index >= 0) { 97 sb.append(" near index "); 98 sb.append(index); 99 } 100 sb.append(System.lineSeparator()); 101 sb.append(pattern); 102 if (index >= 0 && pattern != null && index < pattern.length()) { 103 sb.append(System.lineSeparator()); 104 for (int i = 0; i < index; i++) sb.append(‘ ‘); 105 sb.append(‘^‘); 106 } 107 return sb.toString(); 108 } 109 110 }
原文地址:https://www.cnblogs.com/zhangyishu/p/11263329.html
时间: 2024-10-10 04:10:58