blob: a5edf4798eb5020d93a9ef4a449515f6c156cca8 [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;
Neil Fuller5c92f342019-04-02 20:10:04 +010022import libcore.util.NonNull;
Elliott Hughes41d8acb2014-04-28 19:29:57 -070023
Elliott Hughes5d930ca2014-04-23 17:53:37 -070024/**
Elliott Hughes41d8acb2014-04-28 19:29:57 -070025 * A checked exception thrown when {@link Os} methods fail. This exception contains the native
26 * errno value, for comparison against the constants in {@link OsConstants}, should sophisticated
27 * callers need to adjust their behavior based on the exact failure.
Elliott Hughes5d930ca2014-04-23 17:53:37 -070028 */
Elliott Hughes41d8acb2014-04-28 19:29:57 -070029public final class ErrnoException extends Exception {
Tobias Thierer87231672017-10-18 14:15:38 +010030 private final String functionName;
Elliott Hughes34721e82014-05-06 16:45:47 -070031
Tobias Thierer87231672017-10-18 14:15:38 +010032 /**
33 * The errno value, for comparison with the {@code E} constants in {@link OsConstants}.
34 */
35 public final int errno;
Elliott Hughes41d8acb2014-04-28 19:29:57 -070036
Tobias Thierer87231672017-10-18 14:15:38 +010037 /**
38 * Constructs an instance with the given function name and errno value.
39 */
40 public ErrnoException(String functionName, int errno) {
41 this.functionName = functionName;
42 this.errno = errno;
Elliott Hughes41d8acb2014-04-28 19:29:57 -070043 }
Elliott Hughes41d8acb2014-04-28 19:29:57 -070044
Tobias Thierer87231672017-10-18 14:15:38 +010045 /**
46 * Constructs an instance with the given function name, errno value, and cause.
47 */
48 public ErrnoException(String functionName, int errno, Throwable cause) {
49 super(cause);
50 this.functionName = functionName;
51 this.errno = errno;
52 }
Elliott Hughes41d8acb2014-04-28 19:29:57 -070053
Tobias Thierer87231672017-10-18 14:15:38 +010054 /**
55 * Converts the stashed function name and errno value to a human-readable string.
56 * We do this here rather than in the constructor so that callers only pay for
57 * this if they need it.
58 */
59 @Override public String getMessage() {
60 String errnoName = OsConstants.errnoName(errno);
61 if (errnoName == null) {
62 errnoName = "errno " + errno;
63 }
64 String description = Libcore.os.strerror(errno);
65 return functionName + " failed: " + errnoName + " (" + description + ")";
66 }
67
68 /**
Neil Fuller5c92f342019-04-02 20:10:04 +010069 * Throws an {@link IOException} with a message based on {@link #getMessage()} and with this
70 * instance as the cause.
71 *
72 * <p>This method always terminates by throwing the exception. Callers can write
73 * {@code throw e.rethrowAsIOException()} to make that clear to the compiler.
Tobias Thierer87231672017-10-18 14:15:38 +010074 */
Neil Fuller5c92f342019-04-02 20:10:04 +010075 public @NonNull IOException rethrowAsIOException() throws IOException {
Tobias Thierer87231672017-10-18 14:15:38 +010076 IOException newException = new IOException(getMessage());
77 newException.initCause(this);
78 throw newException;
79 }
80
81 /**
Neil Fuller5c92f342019-04-02 20:10:04 +010082 * Throws a {@link SocketException} with a message based on {@link #getMessage()} and with this
83 * instance as the cause.
84 *
85 * <p>This method always terminates by throwing the exception. Callers can write
86 * {@code throw e.rethrowAsIOException()} to make that clear to the compiler.
Tobias Thierer87231672017-10-18 14:15:38 +010087 */
Neil Fuller5c92f342019-04-02 20:10:04 +010088 public @NonNull SocketException rethrowAsSocketException() throws SocketException {
Tobias Thierer87231672017-10-18 14:15:38 +010089 throw new SocketException(getMessage(), this);
90 }
Elliott Hughes8b15dcc2011-03-29 14:26:04 -070091}