| 1 | /* |
| 2 | * Copyright (C) 2010. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 3 or |
| 6 | * version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | */ |
| 13 | package uk.me.parabola.imgfmt; |
| 14 | |
| 15 | /** |
| 16 | * Used for cases where the current map has failed to compile, but the error |
| 17 | * is expected to be specific to the map (eg it is too big etc). When this |
| 18 | * error is thrown it may be possible for other maps on the command line to |
| 19 | * succeed. |
| 20 | * |
| 21 | * If the error is such that processing further maps is not likely to be |
| 22 | * successful then use {@link ExitException} instead. |
| 23 | * |
| 24 | * @author Steve Ratcliffe |
| 25 | */ |
| 26 | public class MapFailedException extends RuntimeException { |
| 27 | |
| 28 | /** |
| 29 | * Constructs a new runtime exception with the calling method name |
| 30 | * appended to the detail message. |
| 31 | * The cause is not initialized, and may subsequently be initialized by a |
| 32 | * call to {@link #initCause}. |
| 33 | * |
| 34 | * @param message the detail message. The detail message is saved for |
| 35 | * later retrieval by the {@link #getMessage()} method. |
| 36 | */ |
| 37 | public MapFailedException(String message) { |
| 38 | super(buildMessage(message)); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Constructs a new runtime exception with the calling method name |
| 43 | * appended to the detail message and includes the cause. |
| 44 | * <p>Note that the detail message associated with |
| 45 | * <code>cause</code> is <i>not</i> automatically incorporated in |
| 46 | * this runtime exception's detail message. |
| 47 | * |
| 48 | * @param message the detail message (which is saved for later retrieval |
| 49 | * by the {@link #getMessage()} method). |
| 50 | * @param cause the cause (which is saved for later retrieval by the |
| 51 | * {@link #getCause()} method). (A <tt>null</tt> value is |
| 52 | * permitted, and indicates that the cause is nonexistent or |
| 53 | * unknown.) |
| 54 | */ |
| 55 | public MapFailedException(String message, Throwable cause) { |
| 56 | super(buildMessage(message), cause); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Constructs a new runtime exception without appending the calling method |
| 61 | * name to the detail message. The calling method can be appended by the |
| 62 | * derived class if required. |
| 63 | */ |
| 64 | protected MapFailedException(String message, boolean ignored) { |
| 65 | super(message); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Appends the calling method name to the supplied message. |
| 70 | */ |
| 71 | protected static String buildMessage(String message) { |
| 72 | String thrownBy = ""; |
| 73 | try{ |
| 74 | StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); |
| 75 | int callerPosInStack = 3; |
| 76 | String[] caller = stackTraceElements[callerPosInStack].getClassName().split("\\."); |
| 77 | thrownBy = " (thrown in " + caller[caller.length-1]+ "." +stackTraceElements[callerPosInStack].getMethodName() + "())"; |
| 78 | } catch(Exception e){ |
| 79 | } |
| 80 | return message + thrownBy; |
| 81 | } |
| 82 | } |