2 * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.commons.gui.dialog;
10 public class ExceptionDialog {
12 public ExceptionDialog(final Exception exception) {
13 showException(exception);
17 * This method is for testing
19 * @param args commandline arguments
21 public static void main(final String[] args) {
23 final Throwable cause = new Throwable("details.....");
25 final Exception exception = new Exception("test", cause);
27 new ExceptionDialog(exception);
31 * @param exception exception to show
33 public void showException(final Exception exception) {
35 final JFrame frame = new JFrame("Exception occurred!");
36 final JPanel contentPanel = new JPanel(new BorderLayout());
37 final Throwable cause = exception.getCause();
41 final JPanel topPanel = new JPanel();
42 topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
45 final JLabel exceptionType = new JLabel("Exception type: "
46 + exception.getClass().getCanonicalName());
47 topPanel.add(exceptionType);
50 final JLabel message = new JLabel("Error message: "
51 + exception.getMessage());
52 topPanel.add(message);
56 if (cause.getMessage() != null) {
57 final JLabel message2 = new JLabel("Cause: "
58 + cause.getMessage());
59 topPanel.add(message2);
62 contentPanel.add(topPanel, BorderLayout.NORTH);
65 // build stack trace view
67 final StringBuilder buffer = new StringBuilder();
69 // if cause is available, show original stack trace
71 buffer.append("Stack trace:\n");
72 final StackTraceElement[] stackTrace = cause.getStackTrace();
73 for (final StackTraceElement stackTraceElement : stackTrace)
74 buffer.append(stackTraceElement.toString() + "\n");
76 // otherwise show at least current stack trace
77 buffer.append("Stack trace from original cause is not available.\nShowing current stack trace instead:\n");
79 for (final StackTraceElement stackTraceElement : new Exception(
80 "Stack trace").getStackTrace())
81 buffer.append(stackTraceElement.toString() + "\n");
84 final TextArea textArea = new TextArea(buffer.toString());
85 contentPanel.add(textArea, BorderLayout.CENTER);
89 frame.getContentPane().add(contentPanel);
90 frame.setSize(800, 600);
91 frame.setVisible(true);
93 // Thread.dumpStack();