Source code: com/eireneh/util/EventException.java
1
2 package com.eireneh.util;
3
4 import java.util.ResourceBundle;
5 import java.text.*;
6
7 /**
8 * EventExceptions are generally used for passing problems through
9 * the event system which does not allow checked exceptions through.
10 *
11 * <p>So EventException is a LucidException in all but inheritance -
12 * LucidException inherits from Exception and so is checked, where
13 * EventEception inherits from RuntimeException and so is not
14 * checked. In general you would create a subclass of LucidException
15 * before you used it, however EventExceptions would be used directly.
16 * </p>
17 *
18 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
19 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
20 * Distribution Licence:<br />
21 * Project B is free software; you can redistribute it
22 * and/or modify it under the terms of the GNU General Public License,
23 * version 2 as published by the Free Software Foundation.<br />
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.<br />
28 * The License is available on the internet
29 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
30 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
32 * The copyright to this program is held by it's authors.
33 * </font></td></tr></table>
34 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
35 * @see docs.Licence
36 * @see LucidException
37 * @author Joe Walker
38 * @version D0.I0.T0
39 */
40 public class EventException extends RuntimeException
41 {
42 /**
43 * All LucidExceptions are constructed with references to resources in
44 * an I18N properties file.
45 * @param msg The resource id to read
46 */
47 public EventException(String msg)
48 {
49 super(msg);
50
51 this.ex = null;
52 this.params = null;
53 }
54
55 /**
56 * All LucidExceptions are constructed with references to resources in
57 * an I18N properties file.
58 * @param msg The resource id to read
59 */
60 public EventException(String msg, Throwable ex)
61 {
62 super(msg);
63
64 this.ex = ex;
65 this.params = null;
66 }
67
68 /**
69 * All LucidExceptions are constructed with references to resources in
70 * an I18N properties file. This version allows us to add parameters
71 * @param msg The resource id to read
72 * @param params An array of parameters
73 */
74 public EventException(String msg, Object[] params)
75 {
76 super(msg);
77
78 this.ex = null;
79 this.params = params;
80 }
81
82 /**
83 * All LucidExceptions are constructed with references to resources in
84 * an I18N properties file. This version allows us to add parameters
85 * @param msg The resource id to read
86 * @param params An array of parameters
87 */
88 public EventException(String msg, Throwable ex, Object[] params)
89 {
90 super(msg);
91
92 this.ex = ex;
93 this.params = params;
94 }
95
96 /**
97 * We only unravel the message when we need to to save time
98 * @return The unraveled I18N string
99 */
100 public String getMessage()
101 {
102 String id = super.getMessage();
103 String msg;
104
105 try
106 {
107 msg = res.getString(id);
108 }
109 catch (Exception ex)
110 {
111 return "Error fetching resource for '"+id+"'";
112 }
113
114 try
115 {
116 MessageFormat formatter = new MessageFormat(msg);
117 return formatter.format(params);
118 }
119 catch (Exception ex)
120 {
121 return "Error formatting message '"+msg+"'";
122 }
123 }
124
125 /**
126 * The nested Exception (is any)
127 * @return The Exception
128 */
129 public Throwable getException()
130 {
131 return ex;
132 }
133
134 /** An embedded exception */
135 protected Throwable ex;
136
137 /** The array of parameters */
138 protected Object[] params;
139
140 /** The resource hash */
141 protected static ResourceBundle res = ResourceBundle.getBundle("com.eireneh.resources.Exception");
142 }