blob: b434df86b716ffe6691c0a931f6decb9cf251bea [file] [log] [blame]
Elliott Hughes8b15dcc2011-03-29 14:26:04 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes5d930ca2014-04-23 17:53:37 -070017package android.system;
Elliott Hughes8b15dcc2011-03-29 14:26:04 -070018
Elliott Hughes41d8acb2014-04-28 19:29:57 -070019import java.io.IOException;
20import java.net.SocketException;
21import libcore.io.Libcore;
22
Elliott Hughes5d930ca2014-04-23 17:53:37 -070023/**
Elliott Hughes41d8acb2014-04-28 19:29:57 -070024 * A checked exception thrown when {@link Os} methods fail. This exception contains the native
25 * errno value, for comparison against the constants in {@link OsConstants}, should sophisticated
26 * callers need to adjust their behavior based on the exact failure.
27 *
Elliott Hughes5d930ca2014-04-23 17:53:37 -070028 * @hide
29 */
Elliott Hughes41d8acb2014-04-28 19:29:57 -070030public final class ErrnoException extends Exception {
31 private final String functionName;
32 public final int errno;
33
Elliott Hughes5d930ca2014-04-23 17:53:37 -070034 public ErrnoException(String functionName, int errno) {
Elliott Hughes41d8acb2014-04-28 19:29:57 -070035 this.functionName = functionName;
36 this.errno = errno;
Elliott Hughes5d930ca2014-04-23 17:53:37 -070037 }
Elliott Hughes8b15dcc2011-03-29 14:26:04 -070038
Elliott Hughes5d930ca2014-04-23 17:53:37 -070039 public ErrnoException(String functionName, int errno, Throwable cause) {
Elliott Hughes41d8acb2014-04-28 19:29:57 -070040 super(cause);
41 this.functionName = functionName;
42 this.errno = errno;
43 }
44
45 /**
46 * Converts the stashed function name and errno value to a human-readable string.
47 * We do this here rather than in the constructor so that callers only pay for
48 * this if they need it.
49 */
50 @Override public String getMessage() {
51 String errnoName = OsConstants.errnoName(errno);
52 if (errnoName == null) {
53 errnoName = "errno " + errno;
54 }
55 String description = Libcore.os.strerror(errno);
56 return functionName + " failed: " + errnoName + " (" + description + ")";
57 }
58
59 public IOException rethrowAsIOException() throws IOException {
60 IOException newException = new IOException(getMessage());
61 newException.initCause(this);
62 throw newException;
63 }
64
65 public SocketException rethrowAsSocketException() throws SocketException {
66 throw new SocketException(getMessage(), this);
Elliott Hughes5d930ca2014-04-23 17:53:37 -070067 }
Elliott Hughes8b15dcc2011-03-29 14:26:04 -070068}