Path: blob/master/src/java.base/share/classes/java/nio/file/FileSystemException.java
41159 views
/*1* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.file;2627import java.io.IOException;2829/**30* Thrown when a file system operation fails on one or two files. This class is31* the general class for file system exceptions.32*33* @since 1.734*/3536public class FileSystemException37extends IOException38{39@java.io.Serial40static final long serialVersionUID = -3055425747967319812L;4142/**43* String identifying the file or {@code null} if not known.44*/45private final String file;4647/**48* String identifying the other file or {@code null} if there isn't49* another file or if not known.50*/51private final String other;5253/**54* Constructs an instance of this class. This constructor should be used55* when an operation involving one file fails and there isn't any additional56* information to explain the reason.57*58* @param file59* a string identifying the file or {@code null} if not known.60*/61public FileSystemException(String file) {62super((String)null);63this.file = file;64this.other = null;65}6667/**68* Constructs an instance of this class. This constructor should be used69* when an operation involving two files fails, or there is additional70* information to explain the reason.71*72* @param file73* a string identifying the file or {@code null} if not known.74* @param other75* a string identifying the other file or {@code null} if there76* isn't another file or if not known77* @param reason78* a reason message with additional information or {@code null}79*/80public FileSystemException(String file, String other, String reason) {81super(reason);82this.file = file;83this.other = other;84}8586/**87* Returns the file used to create this exception.88*89* @return the file (can be {@code null})90*/91public String getFile() {92return file;93}9495/**96* Returns the other file used to create this exception.97*98* @return the other file (can be {@code null})99*/100public String getOtherFile() {101return other;102}103104/**105* Returns the string explaining why the file system operation failed.106*107* @return the string explaining why the file system operation failed108*/109public String getReason() {110return super.getMessage();111}112113/**114* Returns the detail message string.115*/116@Override117public String getMessage() {118if (file == null && other == null)119return getReason();120StringBuilder sb = new StringBuilder();121if (file != null)122sb.append(file);123if (other != null) {124sb.append(" -> ");125sb.append(other);126}127if (getReason() != null) {128sb.append(": ");129sb.append(getReason());130}131return sb.toString();132}133}134135136