Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / config / IllegalParameterException.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  */
18
19 package peersim.config;
20
21 /**
22 * Exception thrown to indicate that a
23 * configuration property has an invalid value. It is thrown by
24 * several methods in {@link Configuration} and can be thrown by any
25 * component that reads the configuration.
26 */
27 public class IllegalParameterException extends RuntimeException {
28
29 // ================== initialization =====================================
30 // =======================================================================
31
32 /**
33 * Calls super constructor. It passes a string message which is the given
34 * message, prefixed with the given property name.
35 * @param name Name of configuration property that is invalid
36 * @param message Additional info about why the value is invalid
37 */
38 public IllegalParameterException(String name, String message) {
39
40         super("Parameter \"" + name + "\": " + message); 
41 }
42
43 // ================== methods ============================================
44 // =======================================================================
45
46 /**
47 * Extends message with info from stack trace.
48 * It tries to guess what class called {@link Configuration} and
49 * adds relevant info from the stack trace about it to the message.
50 */
51 public String getMessage() {
52
53         StackTraceElement[] stack = getStackTrace();
54
55         // Search the element that invoked Configuration
56         // It's the first whose class is different from Configuration
57         int pos;
58         for (pos=0; pos < stack.length; pos++)
59         {
60                 if (!stack[pos].getClassName().equals(
61                         Configuration.class.getName()))
62                         break;
63         }
64
65         return super.getMessage()+"\nAt "+
66                 getStackTrace()[pos].getClassName()+"."+
67                 getStackTrace()[pos].getMethodName()+":"+
68                 getStackTrace()[pos].getLineNumber();
69 }
70
71 /**
72  * Returns the exception message without stack trace information
73  */
74 public String getShortMessage()
75 {
76         return super.getMessage();
77 }
78
79 }
80