Path: blob/master/src/java.base/unix/classes/sun/nio/fs/UnixException.java
41137 views
/*1* Copyright (c) 2008, 2016, 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 sun.nio.fs;2627import java.nio.file.*;28import java.io.IOException;2930/**31* Internal exception thrown by native methods when error detected.32*/3334class UnixException extends Exception {35static final long serialVersionUID = 7227016794320723218L;3637private int errno;38private String msg;3940UnixException(int errno) {41this.errno = errno;42this.msg = null;43}4445UnixException(String msg) {46this.errno = 0;47this.msg = msg;48}4950int errno() {51return errno;52}5354void setError(int errno) {55this.errno = errno;56this.msg = null;57}5859String errorString() {60if (msg != null) {61return msg;62} else {63return Util.toString(UnixNativeDispatcher.strerror(errno()));64}65}6667@Override68public String getMessage() {69return errorString();70}7172@Override73public Throwable fillInStackTrace() {74// This is an internal exception; the stack trace is irrelevant.75return this;76}7778/**79* Map well known errors to specific exceptions where possible; otherwise80* return more general FileSystemException.81*/82private IOException translateToIOException(String file, String other) {83// created with message rather than errno84if (msg != null)85return new IOException(msg);8687// handle specific cases88if (errno() == UnixConstants.EACCES)89return new AccessDeniedException(file, other, null);90if (errno() == UnixConstants.ENOENT)91return new NoSuchFileException(file, other, null);92if (errno() == UnixConstants.EEXIST)93return new FileAlreadyExistsException(file, other, null);94if (errno() == UnixConstants.ELOOP)95return new FileSystemException(file, other, errorString()96+ " or unable to access attributes of symbolic link");9798// fallback to the more general exception99return new FileSystemException(file, other, errorString());100}101102void rethrowAsIOException(UnixPath file, UnixPath other) throws IOException {103String a = (file == null) ? null : file.getPathForExceptionMessage();104String b = (other == null) ? null : other.getPathForExceptionMessage();105IOException x = translateToIOException(a, b);106throw x;107}108109void rethrowAsIOException(UnixPath file) throws IOException {110rethrowAsIOException(file, null);111}112113IOException asIOException(UnixPath file) {114return translateToIOException(file.getPathForExceptionMessage(), null);115}116}117118119