01: /*
02: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/util/ExceptionUtils.java $
03: * $Revision: 613003 $
04: * $Date: 2008-01-18 00:00:42 +0100 (Fri, 18 Jan 2008) $
05: *
06: * ====================================================================
07: * Licensed to the Apache Software Foundation (ASF) under one
08: * or more contributor license agreements. See the NOTICE file
09: * distributed with this work for additional information
10: * regarding copyright ownership. The ASF licenses this file
11: * to you under the Apache License, Version 2.0 (the
12: * "License"); you may not use this file except in compliance
13: * with the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing,
18: * software distributed under the License is distributed on an
19: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20: * KIND, either express or implied. See the License for the
21: * specific language governing permissions and limitations
22: * under the License.
23: * ====================================================================
24: *
25: * This software consists of voluntary contributions made by many
26: * individuals on behalf of the Apache Software Foundation. For more
27: * information on the Apache Software Foundation, please see
28: * <http://www.apache.org/>.
29: *
30: */
31: package org.apache.http.util;
32:
33: import java.lang.reflect.Method;
34:
35: /**
36: * The home for utility methods that handle various exception-related tasks.
37: *
38: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
39: * @author <a href="mailto:laura@lwerner.org">Laura Werner</a>
40: *
41: * @since 4.0
42: */
43: public final class ExceptionUtils {
44:
45: /** A reference to Throwable's initCause method, or null if it's not there in this JVM */
46: static private final Method INIT_CAUSE_METHOD = getInitCauseMethod();
47:
48: /**
49: * Returns a <code>Method<code> allowing access to
50: * {@link Throwable#initCause(Throwable) initCause} method of {@link Throwable},
51: * or <code>null</code> if the method
52: * does not exist.
53: *
54: * @return A <code>Method<code> for <code>Throwable.initCause</code>, or
55: * <code>null</code> if unavailable.
56: */
57: static private Method getInitCauseMethod() {
58: try {
59: Class[] paramsClasses = new Class[] { Throwable.class };
60: return Throwable.class
61: .getMethod("initCause", paramsClasses);
62: } catch (NoSuchMethodException e) {
63: return null;
64: }
65: }
66:
67: /**
68: * If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
69: *
70: * @param throwable The throwable.
71: * @param cause The cause of the throwable.
72: */
73: public static void initCause(Throwable throwable, Throwable cause) {
74: if (INIT_CAUSE_METHOD != null) {
75: try {
76: INIT_CAUSE_METHOD.invoke(throwable,
77: new Object[] { cause });
78: } catch (Exception e) {
79: // Well, with no logging, the only option is to munch the exception
80: }
81: }
82: }
83:
84: private ExceptionUtils() {
85: }
86:
87: }
|