Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/fs/WindowsException.java
32288 views
/*1* Copyright (c) 2008, 2009, 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}7071private IOException translateToIOException(String file, String other) {72// not created with last error73if (lastError() == 0)74return new IOException(errorString());7576// handle specific cases77if (lastError() == ERROR_FILE_NOT_FOUND || lastError() == ERROR_PATH_NOT_FOUND)78return new NoSuchFileException(file, other, null);79if (lastError() == ERROR_FILE_EXISTS || lastError() == ERROR_ALREADY_EXISTS)80return new FileAlreadyExistsException(file, other, null);81if (lastError() == ERROR_ACCESS_DENIED)82return new AccessDeniedException(file, other, null);8384// fallback to the more general exception85return new FileSystemException(file, other, errorString());86}8788void rethrowAsIOException(String file) throws IOException {89IOException x = translateToIOException(file, null);90throw x;91}9293void rethrowAsIOException(WindowsPath file, WindowsPath other) throws IOException {94String a = (file == null) ? null : file.getPathForExceptionMessage();95String b = (other == null) ? null : other.getPathForExceptionMessage();96IOException x = translateToIOException(a, b);97throw x;98}99100void rethrowAsIOException(WindowsPath file) throws IOException {101rethrowAsIOException(file, null);102}103104IOException asIOException(WindowsPath file) {105return translateToIOException(file.getPathForExceptionMessage(), null);106}107108}109110111