Path: blob/master/src/java.base/windows/classes/sun/nio/fs/WindowsException.java
41139 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;2930import static sun.nio.fs.WindowsConstants.*;3132/**33* Internal exception thrown when a Win32 calls fails.34*/3536class WindowsException extends Exception {37static final long serialVersionUID = 2765039493083748820L;3839private int lastError;40private String msg;4142WindowsException(int lastError) {43this.lastError = lastError;44this.msg = null;45}4647WindowsException(String msg) {48this.lastError = 0;49this.msg = msg;50}5152int lastError() {53return lastError;54}5556String errorString() {57if (msg == null) {58msg = WindowsNativeDispatcher.FormatMessage(lastError);59if (msg == null) {60msg = "Unknown error: 0x" + Integer.toHexString(lastError);61}62}63return msg;64}6566@Override67public String getMessage() {68return errorString();69}7071@Override72public Throwable fillInStackTrace() {73// This is an internal exception; the stack trace is irrelevant.74return this;75}7677private IOException translateToIOException(String file, String other) {78// not created with last error79if (lastError() == 0)80return new IOException(errorString());8182// handle specific cases83if (lastError() == ERROR_FILE_NOT_FOUND || lastError() == ERROR_PATH_NOT_FOUND)84return new NoSuchFileException(file, other, null);85if (lastError() == ERROR_FILE_EXISTS || lastError() == ERROR_ALREADY_EXISTS)86return new FileAlreadyExistsException(file, other, null);87if (lastError() == ERROR_ACCESS_DENIED)88return new AccessDeniedException(file, other, null);8990// fallback to the more general exception91return new FileSystemException(file, other, errorString());92}9394void rethrowAsIOException(String file) throws IOException {95IOException x = translateToIOException(file, null);96throw x;97}9899void rethrowAsIOException(WindowsPath file, WindowsPath other) throws IOException {100String a = (file == null) ? null : file.getPathForExceptionMessage();101String b = (other == null) ? null : other.getPathForExceptionMessage();102IOException x = translateToIOException(a, b);103throw x;104}105106void rethrowAsIOException(WindowsPath file) throws IOException {107rethrowAsIOException(file, null);108}109110IOException asIOException(WindowsPath file) {111return translateToIOException(file.getPathForExceptionMessage(), null);112}113114}115116117