Home | History | Annotate | Download | only in exceptions
      1 /*
      2  * Copyright (C) 2013 DroidDriver committers
      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 io.appium.droiddriver.exceptions;
     18 
     19 /**
     20  * Base exception for DroidDriver.
     21  *
     22  * <p>All exceptions should extend this.
     23  */
     24 @SuppressWarnings("serial")
     25 public class DroidDriverException extends RuntimeException {
     26   public DroidDriverException(String message) {
     27     super(message);
     28   }
     29 
     30   public DroidDriverException(Throwable cause) {
     31     super(cause);
     32   }
     33 
     34   public DroidDriverException(String message, Throwable cause) {
     35     super(message, cause);
     36   }
     37 
     38   /**
     39    * Adapted from <a href="http://guava-libraries.googlecode.com">Guava libraries</a>. <p>
     40    * Propagates {@code throwable} as-is if it is an instance of {@link RuntimeException} or {@link
     41    * Error}, or else as a last resort, wraps it in a {@code DroidDriverException} and then
     42    * propagates. <p> This method always throws an exception. The {@code DroidDriverException} return
     43    * type is only for client code to make Java type system happy in case a return value is required
     44    * by the enclosing method. Example usage:
     45    * <pre>
     46    *   T doSomething() {
     47    *     try {
     48    *       return someMethodThatCouldThrowAnything();
     49    *     } catch (IKnowWhatToDoWithThisException e) {
     50    *       return handle(e);
     51    *     } catch (Throwable t) {
     52    *       throw DroidDriverException.propagate(t);
     53    *     }
     54    *   }
     55    * </pre>
     56    *
     57    * @param throwable the Throwable to propagate
     58    * @return nothing will ever be returned; this return type is only for your convenience, as
     59    * illustrated in the example above
     60    */
     61   public static DroidDriverException propagate(Throwable throwable) {
     62     if (throwable instanceof RuntimeException) {
     63       throw (RuntimeException) throwable;
     64     }
     65     if (throwable instanceof Error) {
     66       throw (Error) throwable;
     67     }
     68     throw new DroidDriverException(throwable);
     69   }
     70 }
     71