org.geotoolkit.util
Class Exceptions

Object
  extended by Static
      extended by Exceptions

public final class Exceptions
extends Static

Utilities methods for dealing with exceptions. Those methods can reformat the stack trace for console output, paint the stack trace in a Graphics2D handler, or show the stack trace in a Swing component.

The Swing component is available only if the geotk-widgets-swing module is available in the classpath. It looks like the following picture:

 

 

Since:
2.0
Version:
3.20
Author:
Martin Desruisseaux (IRD, Geomatys)
Module:
utility/geotk-utility (download)    View source code for this class

Method Summary
static String formatChainedMessages(String header, Throwable cause)
          Returns a string which contain the given message on the first line, followed by the localized message of the given exception on the next line.
static String formatStackTrace(Throwable exception)
          Returns the exception trace as a string.
static void paintStackTrace(Graphics2D graphics, Rectangle widgetBounds, Throwable exception)
          Writes the specified exception trace in the specified graphics context.
static
<T extends Throwable>
T
setMessage(T exception, String message, boolean append)
          Returns an exception of the same kind and with the same stack trace than the given exception, but with a different message.
static void show(Component owner, Throwable exception)
          Displays an error message for the specified exception.
static void show(Component owner, Throwable exception, String message)
          Displays an error message for the specified exception.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setMessage

public static <T extends Throwable> T setMessage(T exception,
                                                 String message,
                                                 boolean append)
Returns an exception of the same kind and with the same stack trace than the given exception, but with a different message. This method simulates the functionality that we would had if Throwable defined a setMessage(String) method. We use this method when an external library throws an exception of the right type, but with too few details.

This method try to create a new exception using reflection. The exception class needs to provide a public constructor expecting a single String argument. If the exception class does not provide such constructor, then the given exception is returned unchanged.

Type Parameters:
T - The type of the exception.
Parameters:
exception - The exception to copy with a different message.
message - The message to set in the exception to be returned.
append - If true, the existing message in the original exception (if any) will be happened after the provided message.
Returns:
A new exception with the given message, or the given exception if the exception class does not provide public Exception(String) constructor.
Since:
3.20

formatChainedMessages

public static String formatChainedMessages(String header,
                                           Throwable cause)
Returns a string which contain the given message on the first line, followed by the localized message of the given exception on the next line. If the exception has a causes, then the localized message of the cause is formatted on the next line and the process is repeated for the whole cause chain.

SQLException is handled especially in order to process the next exception instead than the cause.

This method does not format the stack trace.

Parameters:
header - The message to insert on the first line, or null if none.
cause - The exception, or null if none.
Returns:
The formatted message, or null if both the header was null and no exception provide a message.
Since:
3.01

formatStackTrace

public static String formatStackTrace(Throwable exception)
Returns the exception trace as a string. This method get the stack trace using the Throwable.printStackTrace(PrintWriter) method, then replaces the tabulation characters by 4 white spaces.

Parameters:
exception - The exception to format.
Returns:
A string representation of the given exception.

paintStackTrace

public static void paintStackTrace(Graphics2D graphics,
                                   Rectangle widgetBounds,
                                   Throwable exception)
Writes the specified exception trace in the specified graphics context. This method is useful when an exception has occurred inside a Component.paint(java.awt.Graphics) method and we want to write it rather than leaving an empty window.

Parameters:
graphics - Graphics context in which to write exception. The graphics context shall be in its initial state (default affine transform, default color, etc.)
widgetBounds - Size of the trace which was being drawn.
exception - Exception whose trace we want to write.

show

public static void show(Component owner,
                        Throwable exception)
                 throws UnsupportedOperationException
Displays an error message for the specified exception. Note that this method can be called from any thread (not necessarily the Swing thread).

Parameters:
owner - Component in which the exception is produced, or null if unknown.
exception - Exception which has been thrown and is to be reported to the user.
Throws:
UnsupportedOperationException - If the geotk-widgets-swing module is not found on the classpath.
Since:
3.18 (derived from 1.0)
See Also:
JXErrorPane

show

public static void show(Component owner,
                        Throwable exception,
                        String message)
                 throws UnsupportedOperationException
Displays an error message for the specified exception. Note that this method can be called from any thread (not necessarily the Swing thread).

Parameters:
owner - Component in which the exception is produced, or null if unknown.
exception - Exception which has been thrown and is to be reported to the user.
message - Message to display. If this parameter is null, then Throwable.getLocalizedMessage() will be called to obtain the message.
Throws:
UnsupportedOperationException - If the geotk-widgets-swing module is not found on the classpath.
Since:
3.18 (derived from 1.0)
See Also:
JXErrorPane


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.