1 /* 2 * Copyright (c) 2007, 2009, 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.nio.file; 27 28 /** 29 * Unchecked exception thrown when path string cannot be converted into a 30 * {@link Path} because the path string contains invalid characters, or 31 * the path string is invalid for other file system specific reasons. 32 * 33 * @since 1.7 34 */ 35 36 public class InvalidPathException 37 extends IllegalArgumentException 38 { 39 @java.io.Serial 40 static final long serialVersionUID = 4355821422286746137L; 41 42 private String input; 43 private int index; 44 45 /** 46 * Constructs an instance from the given input string, reason, and error 47 * index. 48 * 49 * @param input the input string 50 * @param reason a string explaining why the input was rejected 51 * @param index the index at which the error occurred, 52 * or {@code -1} if the index is not known 53 * 54 * @throws NullPointerException 55 * if either the input or reason strings are {@code null} 56 * 57 * @throws IllegalArgumentException 58 * if the error index is less than {@code -1} 59 */ 60 public InvalidPathException(String input, String reason, int index) { 61 super(reason); 62 if ((input == null) || (reason == null)) 63 throw new NullPointerException(); 64 if (index < -1) 65 throw new IllegalArgumentException(); 66 this.input = input; 67 this.index = index; 68 } 69 70 /** 71 * Constructs an instance from the given input string and reason. The 72 * resulting object will have an error index of {@code -1}. 73 * 74 * @param input the input string 75 * @param reason a string explaining why the input was rejected 76 * 77 * @throws NullPointerException 78 * if either the input or reason strings are {@code null} 79 */ 80 public InvalidPathException(String input, String reason) { 81 this(input, reason, -1); 82 } 83 84 /** 85 * Returns the input string. 86 * 87 * @return the input string 88 */ 89 public String getInput() { 90 return input; 91 } 92 93 /** 94 * Returns a string explaining why the input string was rejected. 95 * 96 * @return the reason string 97 */ 98 public String getReason() { 99 return super.getMessage(); 100 } 101 102 /** 103 * Returns an index into the input string of the position at which the 104 * error occurred, or {@code -1} if this position is not known. 105 * 106 * @return the error index 107 */ 108 public int getIndex() { 109 return index; 110 } 111 112 /** 113 * Returns a string describing the error. The resulting string 114 * consists of the reason string followed by a colon character 115 * ({@code ':'}), a space, and the input string. If the error index is 116 * defined then the string {@code " at index "} followed by the index, in 117 * decimal, is inserted after the reason string and before the colon 118 * character. 119 * 120 * @return a string describing the error 121 */ 122 public String getMessage() { 123 StringBuilder sb = new StringBuilder(); 124 sb.append(getReason()); 125 if (index > -1) { 126 sb.append(" at index "); 127 sb.append(index); 128 } 129 sb.append(": "); 130 sb.append(input); 131 return sb.toString(); 132 } 133 }