]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ExceptionUtil.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / ExceptionUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.common.utils;
13
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
16
17 /**
18  * Utilities for exception propagation in the database client implementation.
19  * 
20  * @author Tuukka Lehtonen
21  */
22 public final class ExceptionUtil {
23
24     /**
25      * Re-wrap and rethrow a previously caught exception.
26      * 
27      * <p>
28      * Attempts to create a new instance of the specified throwable itself with
29      * the specified Throwable as its cause.
30      * 
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.
34      * 
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
39      *         wrapping failed
40      */
41     public static <T extends Throwable> void wrapAndThrow(T t) throws T {
42         try {
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);
47             throw parent;
48         } catch (InstantiationException e1) {
49             Logger.defaultLogError(e1);
50             throw t;
51         } catch (IllegalAccessException e1) {
52             Logger.defaultLogError(e1);
53             throw t;
54         } catch (IllegalArgumentException e1) {
55             Logger.defaultLogError(e1);
56             throw t;
57         } catch (InvocationTargetException e1) {
58             Logger.defaultLogError(e1.getCause());
59             throw t;
60         } catch (SecurityException e1) {
61             Logger.defaultLogError(e1);
62             throw t;
63         } catch (NoSuchMethodException e1) {
64             Logger.defaultLogError(e1);
65             throw t;
66         }
67     }
68
69 }