1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.common.utils;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
18 * Utilities for exception propagation in the database client implementation.
20 * @author Tuukka Lehtonen
22 public final class ExceptionUtil {
25 * Re-wrap and rethrow a previously caught exception.
28 * Attempts to create a new instance of the specified throwable itself with
29 * the specified Throwable as its cause.
31 * Should the construction of the new throwable fail, the method will log
32 * the error with {@link Logger} and just throw the originally
33 * specified throwable itself.
35 * @param <T> the concrete class of the throwable to be wrapped
36 * @param t the throwable to wrap
37 * @return never returns
38 * @throws T the new wrapping throwable or the original throwable if the
41 public static <T extends Throwable> void wrapAndThrow(T t) throws T {
43 @SuppressWarnings("unchecked")
44 Class<T> clazz = (Class<T>) t.getClass();
45 Constructor<T> ctor = clazz.getConstructor(Throwable.class);
46 T parent = ctor.newInstance(t);
48 } catch (InstantiationException e1) {
49 Logger.defaultLogError(e1);
51 } catch (IllegalAccessException e1) {
52 Logger.defaultLogError(e1);
54 } catch (IllegalArgumentException e1) {
55 Logger.defaultLogError(e1);
57 } catch (InvocationTargetException e1) {
58 Logger.defaultLogError(e1.getCause());
60 } catch (SecurityException e1) {
61 Logger.defaultLogError(e1);
63 } catch (NoSuchMethodException e1) {
64 Logger.defaultLogError(e1);