KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > util > ExceptionUtils


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 package scriptella.util;
17
18 import java.sql.SQLException JavaDoc;
19
20 /**
21  * Utility class to work with throwables.
22  *
23  * @author Fyodor Kupolov
24  * @version 1.0
25  */

26 public final class ExceptionUtils {
27     private ExceptionUtils() {
28     }
29
30     /**
31      * This method checks if throwable has {@link Throwable#getCause() cause}.
32      * If cause==null, this method tries to obtain cause based on throwable type,
33      * e.g. {@link java.sql.SQLException#getNextException()}
34      *
35      * @param throwable throwable to find cause.
36      * @return cause of throwable.
37      */

38     public static Throwable JavaDoc getCause(Throwable JavaDoc throwable) {
39         if (throwable.getCause() != null) {
40             return throwable.getCause();
41         }
42         if (throwable instanceof SQLException JavaDoc) {
43             return ((SQLException JavaDoc) throwable).getNextException();
44         }
45         return null;
46     }
47
48     /**
49      * This method throws unchecked throwable, i.e. {@link Error} or {@link RuntimeException}.
50      *
51      * @param unchecked unchecked throwable.
52      */

53     public static void throwUnchecked(Throwable JavaDoc unchecked) {
54         if (unchecked instanceof Error JavaDoc) {
55             throw (Error JavaDoc) unchecked;
56         }
57         if (unchecked instanceof RuntimeException JavaDoc) {
58             throw (RuntimeException JavaDoc) unchecked;
59         }
60         throw new IllegalStateException JavaDoc("Unchecked throwable expected but was " + unchecked.getClass(), unchecked);
61     }
62
63     /**
64      * Utility method to ignore non important exceptions.
65      * @param throwable throwable to ignore.
66      */

67     public static void ignoreThrowable(Throwable JavaDoc throwable) {
68     }
69
70 }
71
Popular Tags