2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2020, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.commons.gui.dialog;
15 public class ExceptionDialog {
17 public ExceptionDialog(final Exception exception) {
18 showException(exception);
22 * This method is for testing
24 * @param args commandline arguments
26 public static void main(final String[] args) {
28 final Throwable cause = new Throwable("details.....");
30 final Exception exception = new Exception("test", cause);
32 new ExceptionDialog(exception);
36 * @param exception exception to show
38 public void showException(final Exception exception) {
40 final JFrame frame = new JFrame("Exception occurred!");
41 final JPanel contentPanel = new JPanel(new BorderLayout());
42 final Throwable cause = exception.getCause();
46 final JPanel topPanel = new JPanel();
47 topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
50 final JLabel exceptionType = new JLabel("Exception type: "
51 + exception.getClass().getCanonicalName());
52 topPanel.add(exceptionType);
55 final JLabel message = new JLabel("Error message: "
56 + exception.getMessage());
57 topPanel.add(message);
61 if (cause.getMessage() != null) {
62 final JLabel message2 = new JLabel("Cause: "
63 + cause.getMessage());
64 topPanel.add(message2);
67 contentPanel.add(topPanel, BorderLayout.NORTH);
70 // build stack trace view
72 final StringBuilder buffer = new StringBuilder();
74 // if cause is available, show original stack trace
76 buffer.append("Stack trace:\n");
77 final StackTraceElement[] stackTrace = cause.getStackTrace();
78 for (final StackTraceElement stackTraceElement : stackTrace)
79 buffer.append(stackTraceElement.toString() + "\n");
81 // otherwise show at least current stack trace
82 buffer.append("Stack trace from original cause is not available.\nShowing current stack trace instead:\n");
84 for (final StackTraceElement stackTraceElement : new Exception(
85 "Stack trace").getStackTrace())
86 buffer.append(stackTraceElement.toString() + "\n");
89 final TextArea textArea = new TextArea(buffer.toString());
90 contentPanel.add(textArea, BorderLayout.CENTER);
94 frame.getContentPane().add(contentPanel);
95 frame.setSize(800, 600);
96 frame.setVisible(true);
98 // Thread.dumpStack();