Elliott Hughes | 8b15dcc | 2011-03-29 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 5d930ca | 2014-04-23 17:53:37 -0700 | [diff] [blame] | 17 | package android.system; |
Elliott Hughes | 8b15dcc | 2011-03-29 14:26:04 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | import java.net.SocketException; |
| 21 | import libcore.io.Libcore; |
Neil Fuller | 5c92f34 | 2019-04-02 20:10:04 +0100 | [diff] [blame^] | 22 | import libcore.util.NonNull; |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 23 | |
Elliott Hughes | 5d930ca | 2014-04-23 17:53:37 -0700 | [diff] [blame] | 24 | /** |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 25 | * 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 Hughes | 5d930ca | 2014-04-23 17:53:37 -0700 | [diff] [blame] | 28 | */ |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 29 | public final class ErrnoException extends Exception { |
Tobias Thierer | 8723167 | 2017-10-18 14:15:38 +0100 | [diff] [blame] | 30 | private final String functionName; |
Elliott Hughes | 34721e8 | 2014-05-06 16:45:47 -0700 | [diff] [blame] | 31 | |
Tobias Thierer | 8723167 | 2017-10-18 14:15:38 +0100 | [diff] [blame] | 32 | /** |
| 33 | * The errno value, for comparison with the {@code E} constants in {@link OsConstants}. |
| 34 | */ |
| 35 | public final int errno; |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 36 | |
Tobias Thierer | 8723167 | 2017-10-18 14:15:38 +0100 | [diff] [blame] | 37 | /** |
| 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 Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 43 | } |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 44 | |
Tobias Thierer | 8723167 | 2017-10-18 14:15:38 +0100 | [diff] [blame] | 45 | /** |
| 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 Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 53 | |
Tobias Thierer | 8723167 | 2017-10-18 14:15:38 +0100 | [diff] [blame] | 54 | /** |
| 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 Fuller | 5c92f34 | 2019-04-02 20:10:04 +0100 | [diff] [blame^] | 69 | * 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 Thierer | 8723167 | 2017-10-18 14:15:38 +0100 | [diff] [blame] | 74 | */ |
Neil Fuller | 5c92f34 | 2019-04-02 20:10:04 +0100 | [diff] [blame^] | 75 | public @NonNull IOException rethrowAsIOException() throws IOException { |
Tobias Thierer | 8723167 | 2017-10-18 14:15:38 +0100 | [diff] [blame] | 76 | IOException newException = new IOException(getMessage()); |
| 77 | newException.initCause(this); |
| 78 | throw newException; |
| 79 | } |
| 80 | |
| 81 | /** |
Neil Fuller | 5c92f34 | 2019-04-02 20:10:04 +0100 | [diff] [blame^] | 82 | * 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 Thierer | 8723167 | 2017-10-18 14:15:38 +0100 | [diff] [blame] | 87 | */ |
Neil Fuller | 5c92f34 | 2019-04-02 20:10:04 +0100 | [diff] [blame^] | 88 | public @NonNull SocketException rethrowAsSocketException() throws SocketException { |
Tobias Thierer | 8723167 | 2017-10-18 14:15:38 +0100 | [diff] [blame] | 89 | throw new SocketException(getMessage(), this); |
| 90 | } |
Elliott Hughes | 8b15dcc | 2011-03-29 14:26:04 -0700 | [diff] [blame] | 91 | } |