001    /**
002     Licensed to the Apache Software Foundation (ASF) under one or more
003     contributor license agreements.  See the NOTICE file distributed with
004     this work for additional information regarding copyright ownership.
005     The ASF licenses this file to You under the Apache License, Version 2.0
006     (the "License"); you may not use this file except in compliance with
007     the License.  You may obtain a copy of the License at
008    
009          http://www.apache.org/licenses/LICENSE-2.0
010    
011     Unless required by applicable law or agreed to in writing, software
012     distributed under the License is distributed on an "AS IS" BASIS,
013     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     See the License for the specific language governing permissions and
015     limitations under the License.
016    */
017    package org.apache.commons.inject.util;
018    
019    import java.lang.reflect.UndeclaredThrowableException;
020    
021    
022    /**
023     * A utility class for dealing with Exceptions.
024     */
025    public class Exceptions {
026            /**
027             * Throws the given exception, or another exception, which doesn't affect
028             * the method signature.
029             * @param pTh The {@link Throwable} to show. If this is an instance of
030             *   {@link RuntimeException}, or {@link Error}, then this Throwable itself
031             *   will be thrown. Otherwise, the Throwable will be wrapped into an instance
032             *   of {@link UndeclaredThrowableException}, and that will be thrown.
033             * @return Nothing, an exception will always be thrown: This method is
034             *   effectively void. To declare it otherwise makes it possible to write
035             *   {@code throw show(myThrowable);}, which allows the compiler to detect
036             *   what's happening.
037             */
038            public static RuntimeException show(Throwable pTh) {
039                    if (pTh == null) {
040                            return new NullPointerException("The Throwable to show must not be null.");
041                    } else if (pTh instanceof RuntimeException) {
042                            return (RuntimeException) pTh;
043                    } else if (pTh instanceof Error) {
044                            throw (Error) pTh;
045                    } else {
046                            return new UndeclaredThrowableException(pTh);
047                    }
048            }
049    }