• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2   * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/ExceptionUtils.java $
3   * $Revision: 613003 $
4   * $Date: 2008-01-17 15:00:42 -0800 (Thu, 17 Jan 2008) $
5   *
6   * ====================================================================
7   * Licensed to the Apache Software Foundation (ASF) under one
8   * or more contributor license agreements.  See the NOTICE file
9   * 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       */
getInitCauseMethod()57      static private Method getInitCauseMethod() {
58          try {
59              Class[] paramsClasses = new Class[] { Throwable.class };
60              return Throwable.class.getMethod("initCause", paramsClasses);
61          } catch (NoSuchMethodException e) {
62              return null;
63          }
64      }
65  
66      /**
67       * If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
68       *
69       * @param  throwable The throwable.
70       * @param  cause     The cause of the throwable.
71       */
initCause(Throwable throwable, Throwable cause)72      public static void initCause(Throwable throwable, Throwable cause) {
73          if (INIT_CAUSE_METHOD != null) {
74              try {
75                  INIT_CAUSE_METHOD.invoke(throwable, new Object[] { cause });
76              } catch (Exception e) {
77                  // Well, with no logging, the only option is to munch the exception
78              }
79          }
80      }
81  
ExceptionUtils()82      private ExceptionUtils() {
83      }
84  
85  }
86