I have a Java Swing program with many classes. I do use try-catchs where they are required, but no where else. I also have a logger class, which writes to a file when an exception is caught. It prints out the stack trace and the message.
I want to be able to log every exception but without putting try-catch's everywhere. Is this possible?
-- EDIT (My main method) --
public class Bacon extends Thread implements UncaughtExceptionHandler {
    public static Bacon instance = null;
    private JFrame main;
    private Bacon() {
        main = new JFrame("Bacon");
        main.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        main.addWindowListener(new WindowEventHandler());
        setLAF();
        main.setSize(Constants.MAIN_DIMENSION);
        main.setLocationRelativeTo(null);
        main.setExtendedState(JFrame.MAXIMIZED_BOTH);
        main.setIconImage(getIcon());
        setUserName();
        setUncaughtExceptionHandler(this);
        main.setJMenuBar(Menu.getInstance());
        main.setContentPane(getMainPanel());
        main.setVisible(true);
    }
    public static Bacon getInstance() {
        if(instance == null)
            instance = new Bacon();
        return instance;
    }
    private JPanel getMainPanel() {
        JPanel main = new JPanel(new BorderLayout());
        main.add(Tabs.getInstance(), BorderLayout.CENTER);
        main.add(StatusBar.getInstance(), BorderLayout.SOUTH);
        return main;
    }
    .
    .
    .
    .
    .
    .
    .
    public static void main(String[] args) {
        try {
            getInstance();
        } catch (Throwable t) {
            ErrorLogging.log(t.getStackTrace(), t.getMessage());
        }
    }
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        ErrorLogging.log(e.getStackTrace(), e.getMessage());
    }
}
Absolutely!
Look at the Thread class' method setDefaultUncaughtExceptionHandler.
Thread.setDefaultUncaughtExceptionHandler
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With