Home | History | Annotate | Download | only in system
      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 package android.system;
     18 
     19 import java.io.IOException;
     20 import java.net.SocketException;
     21 import libcore.io.Libcore;
     22 
     23 /**
     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  */
     28 public final class ErrnoException extends Exception {
     29   private final String functionName;
     30 
     31   /**
     32    * The errno value, for comparison with the {@code E} constants in {@link OsConstants}.
     33    */
     34   public final int errno;
     35 
     36   /**
     37    * Constructs an instance with the given function name and errno value.
     38    */
     39   public ErrnoException(String functionName, int errno) {
     40     this.functionName = functionName;
     41     this.errno = errno;
     42   }
     43 
     44   /**
     45    * Constructs an instance with the given function name, errno value, and cause.
     46    */
     47   public ErrnoException(String functionName, int errno, Throwable cause) {
     48     super(cause);
     49     this.functionName = functionName;
     50     this.errno = errno;
     51   }
     52 
     53   /**
     54    * Converts the stashed function name and errno value to a human-readable string.
     55    * We do this here rather than in the constructor so that callers only pay for
     56    * this if they need it.
     57    */
     58   @Override public String getMessage() {
     59     String errnoName = OsConstants.errnoName(errno);
     60     if (errnoName == null) {
     61       errnoName = "errno " + errno;
     62     }
     63     String description = Libcore.os.strerror(errno);
     64     return functionName + " failed: " + errnoName + " (" + description + ")";
     65   }
     66 
     67   /**
     68    * @hide - internal use only.
     69    */
     70   public IOException rethrowAsIOException() throws IOException {
     71     IOException newException = new IOException(getMessage());
     72     newException.initCause(this);
     73     throw newException;
     74   }
     75 
     76   /**
     77    * @hide - internal use only.
     78    */
     79   public SocketException rethrowAsSocketException() throws SocketException {
     80     throw new SocketException(getMessage(), this);
     81   }
     82 }
     83