1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package java.net; 19 20 /** 21 * A {@code URISyntaxException} will be thrown if some information could not be parsed 22 * while creating a URI. 23 */ 24 public class URISyntaxException extends Exception { 25 26 private static final long serialVersionUID = 2137979680897488891L; 27 28 private String input; 29 30 private int index; 31 32 /** 33 * Constructs a new {@code URISyntaxException} instance containing the 34 * string that caused the exception, a description of the problem and the 35 * index at which the error occurred. 36 * 37 * @param input 38 * the string that caused the exception. 39 * @param reason 40 * the reason why the exception occurred. 41 * @param index 42 * the position where the exception occurred. 43 * @throws NullPointerException 44 * if one of the arguments {@code input} or {@code reason} is 45 * {@code null}. 46 * @throws IllegalArgumentException 47 * if the value for {@code index} is lesser than {@code -1}. 48 */ URISyntaxException(String input, String reason, int index)49 public URISyntaxException(String input, String reason, int index) { 50 super(reason); 51 52 if (input == null) { 53 throw new NullPointerException("input == null"); 54 } else if (reason == null) { 55 throw new NullPointerException("reason == null"); 56 } 57 58 if (index < -1) { 59 throw new IllegalArgumentException("Bad index: " + index); 60 } 61 62 this.input = input; 63 this.index = index; 64 } 65 66 /** 67 * Constructs a new {@code URISyntaxException} instance containing the 68 * string that caused the exception and a description of the problem. 69 * 70 *@param input 71 * the string that caused the exception. 72 * @param reason 73 * the reason why the exception occurred. 74 * @throws NullPointerException 75 * if one of the arguments {@code input} or {@code reason} is 76 * {@code null}. 77 */ URISyntaxException(String input, String reason)78 public URISyntaxException(String input, String reason) { 79 super(reason); 80 81 if (input == null) { 82 throw new NullPointerException("input == null"); 83 } else if (reason == null) { 84 throw new NullPointerException("reason == null"); 85 } 86 87 this.input = input; 88 index = -1; 89 } 90 91 /** 92 * Gets the index at which the syntax error was found or {@code -1} if the 93 * index is unknown/unavailable. 94 * 95 * @return the index of the syntax error. 96 */ getIndex()97 public int getIndex() { 98 return index; 99 } 100 101 /** 102 * Gets a description of the syntax error. 103 * 104 * @return the string describing the syntax error. 105 */ getReason()106 public String getReason() { 107 return super.getMessage(); 108 } 109 110 /** 111 * Gets the initial string that contains an invalid syntax. 112 * 113 * @return the string that caused the exception. 114 */ getInput()115 public String getInput() { 116 return input; 117 } 118 119 /** 120 * Gets a description of the exception, including the reason, the string 121 * that caused the syntax error and the position of the syntax error if 122 * available. 123 * 124 * @return a sting containing information about the exception. 125 * @see java.lang.Throwable#getMessage() 126 */ 127 @Override getMessage()128 public String getMessage() { 129 String reason = super.getMessage(); 130 if (index != -1) { 131 return reason + " at index " + index + ": " + input; 132 } 133 return reason + ": " + input; 134 } 135 } 136