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; |
| 32 | public final int errno; |
| 33 | |
Elliott Hughes | 5d930ca | 2014-04-23 17:53:37 -0700 | [diff] [blame] | 34 | public ErrnoException(String functionName, int errno) { |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 35 | this.functionName = functionName; |
| 36 | this.errno = errno; |
Elliott Hughes | 5d930ca | 2014-04-23 17:53:37 -0700 | [diff] [blame] | 37 | } |
Elliott Hughes | 8b15dcc | 2011-03-29 14:26:04 -0700 | [diff] [blame] | 38 | |
Elliott Hughes | 5d930ca | 2014-04-23 17:53:37 -0700 | [diff] [blame] | 39 | public ErrnoException(String functionName, int errno, Throwable cause) { |
Elliott Hughes | 41d8acb | 2014-04-28 19:29:57 -0700 | [diff] [blame] | 40 | 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 Hughes | 5d930ca | 2014-04-23 17:53:37 -0700 | [diff] [blame] | 67 | } |
Elliott Hughes | 8b15dcc | 2011-03-29 14:26:04 -0700 | [diff] [blame] | 68 | } |