• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 // [ravenwood] Copied from libcore.
18 
19 package android.system;
20 
21 import java.io.IOException;
22 import java.net.SocketException;
23 
24 /**
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.
28  */
29 public final class ErrnoException extends Exception {
30     private final String functionName;
31 
32     /**
33      * The errno value, for comparison with the {@code E} constants in {@link OsConstants}.
34      */
35     public final int errno;
36 
37     /**
38      * Constructs an instance with the given function name and errno value.
39      */
ErrnoException(String functionName, int errno)40     public ErrnoException(String functionName, int errno) {
41         this.functionName = functionName;
42         this.errno = errno;
43     }
44 
45     /**
46      * Constructs an instance with the given function name, errno value, and cause.
47      */
ErrnoException(String functionName, int errno, Throwable cause)48     public ErrnoException(String functionName, int errno, Throwable cause) {
49         super(cause);
50         this.functionName = functionName;
51         this.errno = errno;
52     }
53 
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      */
getMessage()59     @Override public String getMessage() {
60         return functionName + " failed: " + errno;
61     }
62 
63     /**
64      * Throws an {@link IOException} with a message based on {@link #getMessage()} and with this
65      * instance as the cause.
66      *
67      * <p>This method always terminates by throwing the exception. Callers can write
68      * {@code throw e.rethrowAsIOException()} to make that clear to the compiler.
69      */
rethrowAsIOException()70     public IOException rethrowAsIOException() throws IOException {
71         IOException newException = new IOException(getMessage());
72         newException.initCause(this);
73         throw newException;
74     }
75 
76     /**
77      * Throws a {@link SocketException} with a message based on {@link #getMessage()} and with this
78      * instance as the cause.
79      *
80      * <p>This method always terminates by throwing the exception. Callers can write
81      * {@code throw e.rethrowAsIOException()} to make that clear to the compiler.
82      */
rethrowAsSocketException()83     public SocketException rethrowAsSocketException() throws SocketException {
84         throw new SocketException(getMessage());
85     }
86 }
87