Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Exceptions |
|
| 7.0;7 |
1 | /** | |
2 | Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | contributor license agreements. See the NOTICE file distributed with | |
4 | this work for additional information regarding copyright ownership. | |
5 | The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | (the "License"); you may not use this file except in compliance with | |
7 | the License. You may obtain a copy of the License at | |
8 | ||
9 | http://www.apache.org/licenses/LICENSE-2.0 | |
10 | ||
11 | Unless required by applicable law or agreed to in writing, software | |
12 | distributed under the License is distributed on an "AS IS" BASIS, | |
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | See the License for the specific language governing permissions and | |
15 | limitations under the License. | |
16 | */ | |
17 | package org.apache.commons.inject.util; | |
18 | ||
19 | import java.lang.reflect.UndeclaredThrowableException; | |
20 | ||
21 | ||
22 | /** | |
23 | * A utility class for dealing with Exceptions. | |
24 | */ | |
25 | 0 | public class Exceptions { |
26 | /** | |
27 | * Throws the given exception, or another exception, which doesn't affect | |
28 | * the method signature. | |
29 | * @param pTh The {@link Throwable} to show. If this is an instance of | |
30 | * {@link RuntimeException}, or {@link Error}, then this Throwable itself | |
31 | * will be thrown. Otherwise, the Throwable will be wrapped into an instance | |
32 | * of {@link UndeclaredThrowableException}, and that will be thrown. | |
33 | * @return Nothing, an exception will always be thrown: This method is | |
34 | * effectively void. To declare it otherwise makes it possible to write | |
35 | * {@code throw show(myThrowable);}, which allows the compiler to detect | |
36 | * what's happening. | |
37 | */ | |
38 | public static RuntimeException show(Throwable pTh) { | |
39 | 3 | if (pTh == null) { |
40 | 0 | return new NullPointerException("The Throwable to show must not be null."); |
41 | 3 | } else if (pTh instanceof RuntimeException) { |
42 | 1 | return (RuntimeException) pTh; |
43 | 2 | } else if (pTh instanceof Error) { |
44 | 1 | throw (Error) pTh; |
45 | } else { | |
46 | 1 | return new UndeclaredThrowableException(pTh); |
47 | } | |
48 | } | |
49 | } |