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; |
| 22 | |
Elliott Hughes | 5d930ca | 2014-04-23 17:53:37 -0700 | [diff] [blame] | 23 | /** |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 24 | * 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 Hughes | 5d930ca | 2014-04-23 17:53:37 -0700 | [diff] [blame] | 28 | * @hide |
| 29 | */ |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 30 | public final class ErrnoException extends Exception { |
| 31 | private final String functionName; |
Elliott Hughes | 34721e8 | 2014-05-06 16:45:47 -0700 | [diff] [blame^] | 32 | |
| 33 | /** |
| 34 | * The errno value, for comparison with the {@code E} constants in {@link OsConstants}. |
| 35 | */ |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 36 | public final int errno; |
| 37 | |
Elliott Hughes | 34721e8 | 2014-05-06 16:45:47 -0700 | [diff] [blame^] | 38 | /** |
| 39 | * Constructs an instance with the given function name and errno value. |
| 40 | */ |
Elliott Hughes | 5d930ca | 2014-04-23 17:53:37 -0700 | [diff] [blame] | 41 | public ErrnoException(String functionName, int errno) { |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 42 | this.functionName = functionName; |
| 43 | this.errno = errno; |
Elliott Hughes | 5d930ca | 2014-04-23 17:53:37 -0700 | [diff] [blame] | 44 | } |
Elliott Hughes | 8b15dcc | 2011-03-29 14:26:04 -0700 | [diff] [blame] | 45 | |
Elliott Hughes | 34721e8 | 2014-05-06 16:45:47 -0700 | [diff] [blame^] | 46 | /** |
| 47 | * Constructs an instance with the given function name, errno value, and cause. |
| 48 | */ |
Elliott Hughes | 5d930ca | 2014-04-23 17:53:37 -0700 | [diff] [blame] | 49 | public ErrnoException(String functionName, int errno, Throwable cause) { |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 50 | 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 Hughes | 34721e8 | 2014-05-06 16:45:47 -0700 | [diff] [blame^] | 69 | /** |
| 70 | * @hide - internal use only. |
| 71 | */ |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 72 | public IOException rethrowAsIOException() throws IOException { |
| 73 | IOException newException = new IOException(getMessage()); |
| 74 | newException.initCause(this); |
| 75 | throw newException; |
| 76 | } |
| 77 | |
Elliott Hughes | 34721e8 | 2014-05-06 16:45:47 -0700 | [diff] [blame^] | 78 | /** |
| 79 | * @hide - internal use only. |
| 80 | */ |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 81 | public SocketException rethrowAsSocketException() throws SocketException { |
| 82 | throw new SocketException(getMessage(), this); |
Elliott Hughes | 5d930ca | 2014-04-23 17:53:37 -0700 | [diff] [blame] | 83 | } |
Elliott Hughes | 8b15dcc | 2011-03-29 14:26:04 -0700 | [diff] [blame] | 84 | } |