blob: 134d6a0450f60e080ca2544b8353daca375db9c2 [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;
Elliott Hughes34721e82014-05-06 16:45:47 -070032
33 /**
34 * The errno value, for comparison with the {@code E} constants in {@link OsConstants}.
35 */
Elliott Hughes41d8acb2014-04-28 19:29:57 -070036 public final int errno;
37
Elliott Hughes34721e82014-05-06 16:45:47 -070038 /**
39 * Constructs an instance with the given function name and errno value.
40 */
Elliott Hughes5d930ca2014-04-23 17:53:37 -070041 public ErrnoException(String functionName, int errno) {
Elliott Hughes41d8acb2014-04-28 19:29:57 -070042 this.functionName = functionName;
43 this.errno = errno;
Elliott Hughes5d930ca2014-04-23 17:53:37 -070044 }
Elliott Hughes8b15dcc2011-03-29 14:26:04 -070045
Elliott Hughes34721e82014-05-06 16:45:47 -070046 /**
47 * Constructs an instance with the given function name, errno value, and cause.
48 */
Elliott Hughes5d930ca2014-04-23 17:53:37 -070049 public ErrnoException(String functionName, int errno, Throwable cause) {
Elliott Hughes41d8acb2014-04-28 19:29:57 -070050 super(cause);
51 this.functionName = functionName;
52 this.errno = errno;
53 }
54
55 /**
56 * Converts the stashed function name and errno value to a human-readable string.
57 * We do this here rather than in the constructor so that callers only pay for
58 * this if they need it.
59 */
60 @Override public String getMessage() {
61 String errnoName = OsConstants.errnoName(errno);
62 if (errnoName == null) {
63 errnoName = "errno " + errno;
64 }
65 String description = Libcore.os.strerror(errno);
66 return functionName + " failed: " + errnoName + " (" + description + ")";
67 }
68
Elliott Hughes34721e82014-05-06 16:45:47 -070069 /**
70 * @hide - internal use only.
71 */
Elliott Hughes41d8acb2014-04-28 19:29:57 -070072 public IOException rethrowAsIOException() throws IOException {
73 IOException newException = new IOException(getMessage());
74 newException.initCause(this);
75 throw newException;
76 }
77
Elliott Hughes34721e82014-05-06 16:45:47 -070078 /**
79 * @hide - internal use only.
80 */
Elliott Hughes41d8acb2014-04-28 19:29:57 -070081 public SocketException rethrowAsSocketException() throws SocketException {
82 throw new SocketException(getMessage(), this);
Elliott Hughes5d930ca2014-04-23 17:53:37 -070083 }
Elliott Hughes8b15dcc2011-03-29 14:26:04 -070084}