View Javadoc

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