1 /* 2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.net; 27 28 29 /** 30 * Checked exception thrown to indicate that a string could not be parsed as a 31 * URI reference. 32 * 33 * @author Mark Reinhold 34 * @see URI 35 * @since 1.4 36 */ 37 38 public class URISyntaxException 39 extends Exception 40 { 41 @java.io.Serial 42 private static final long serialVersionUID = 2137979680897488891L; 43 44 private String input; 45 private int index; 46 47 /** 48 * Constructs an instance from the given input string, reason, and error 49 * index. 50 * 51 * @param input The input string 52 * @param reason A string explaining why the input could not be parsed 53 * @param index The index at which the parse error occurred, 54 * or {@code -1} if the index is not known 55 * 56 * @throws NullPointerException 57 * If either the input or reason strings are {@code null} 58 * 59 * @throws IllegalArgumentException 60 * If the error index is less than {@code -1} 61 */ 62 public URISyntaxException(String input, String reason, int index) { 63 super(reason); 64 if ((input == null) || (reason == null)) 65 throw new NullPointerException(); 66 if (index < -1) 67 throw new IllegalArgumentException(); 68 this.input = input; 69 this.index = index; 70 } 71 72 /** 73 * Constructs an instance from the given input string and reason. The 74 * resulting object will have an error index of {@code -1}. 75 * 76 * @param input The input string 77 * @param reason A string explaining why the input could not be parsed 78 * 79 * @throws NullPointerException 80 * If either the input or reason strings are {@code null} 81 */ 82 public URISyntaxException(String input, String reason) { 83 this(input, reason, -1); 84 } 85 86 /** 87 * Returns the input string. 88 * 89 * @return The input string 90 */ 91 public String getInput() { 92 return input; 93 } 94 95 /** 96 * Returns a string explaining why the input string could not be parsed. 97 * 98 * @return The reason string 99 */ 100 public String getReason() { 101 return super.getMessage(); 102 } 103 104 /** 105 * Returns an index into the input string of the position at which the 106 * parse error occurred, or {@code -1} if this position is not known. 107 * 108 * @return The error index 109 */ 110 public int getIndex() { 111 return index; 112 } 113 114 /** 115 * Returns a string describing the parse error. The resulting string 116 * consists of the reason string followed by a colon character 117 * ({@code ':'}), a space, and the input string. If the error index is 118 * defined then the string {@code " at index "} followed by the index, in 119 * decimal, is inserted after the reason string and before the colon 120 * character. 121 * 122 * @return A string describing the parse error 123 */ 124 public String getMessage() { 125 StringBuilder sb = new StringBuilder(); 126 sb.append(getReason()); 127 if (index > -1) { 128 sb.append(" at index "); 129 sb.append(index); 130 } 131 sb.append(": "); 132 sb.append(input); 133 return sb.toString(); 134 } 135 136 }