]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ExceptionUtil.java
Sync git svn branch with SVN repository r33144.
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / ExceptionUtil.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.common.utils;\r
13 \r
14 import java.lang.reflect.Constructor;\r
15 import java.lang.reflect.InvocationTargetException;\r
16 \r
17 /**\r
18  * Utilities for exception propagation in the database client implementation.\r
19  * \r
20  * @author Tuukka Lehtonen\r
21  */\r
22 public final class ExceptionUtil {\r
23 \r
24     /**\r
25      * Re-wrap and rethrow a previously caught exception.\r
26      * \r
27      * <p>\r
28      * Attempts to create a new instance of the specified throwable itself with\r
29      * the specified Throwable as its cause.\r
30      * \r
31      * Should the construction of the new throwable fail, the method will log\r
32      * the error with {@link Logger} and just throw the originally\r
33      * specified throwable itself.\r
34      * \r
35      * @param <T> the concrete class of the throwable to be wrapped\r
36      * @param t the throwable to wrap\r
37      * @return never returns\r
38      * @throws T the new wrapping throwable or the original throwable if the\r
39      *         wrapping failed\r
40      */\r
41     public static <T extends Throwable> void wrapAndThrow(T t) throws T {\r
42         try {\r
43             @SuppressWarnings("unchecked")\r
44             Class<T> clazz = (Class<T>) t.getClass();\r
45             Constructor<T> ctor = clazz.getConstructor(Throwable.class);\r
46             T parent = ctor.newInstance(t);\r
47             throw parent;\r
48         } catch (InstantiationException e1) {\r
49             Logger.defaultLogError(e1);\r
50             throw t;\r
51         } catch (IllegalAccessException e1) {\r
52             Logger.defaultLogError(e1);\r
53             throw t;\r
54         } catch (IllegalArgumentException e1) {\r
55             Logger.defaultLogError(e1);\r
56             throw t;\r
57         } catch (InvocationTargetException e1) {\r
58             Logger.defaultLogError(e1.getCause());\r
59             throw t;\r
60         } catch (SecurityException e1) {\r
61             Logger.defaultLogError(e1);\r
62             throw t;\r
63         } catch (NoSuchMethodException e1) {\r
64             Logger.defaultLogError(e1);\r
65             throw t;\r
66         }\r
67     }\r
68 \r
69 }\r