How do I listen for errors in the log?
I am trying to figure out how I will listen to bugs in Java. What I want to do is an error that is being thrown to the console, the program will detect it and make it easier (stack traces can be scary!).
Here's what I've tried:
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException(Thread t, Throwable e)
{
// do some good stuff here, like logging or sending an alert email to the Support team
System.out.println("EXCEPTION CAUGHT!");
System.out.println(e.getMessage());
}
});
But unfortunately that doesn't say EXCEPTION CAUGHT! when an error occurs. Do I have to do it this way or is there a way to listen to the System.out log itself when it is updated?
source to share
For lack of an answer, but a lot of discussion, which is probably going on in your framework, is setting up a custom handler that swallows your exception (for example, almost every web framework).
Here's some code to illustrate how it works, since there is a default handler and then each thread or group of threads can have its own handler.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Tests
{
public static void main(String ... args)
{
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException(Thread t, Throwable e)
{
System.out.print("[Handler Handling]\t");
System.out.print(t.getName());
System.out.print("\n");
e.printStackTrace(System.out);
System.out.println("[End Handling]");
}
});
new Thread()
{
{
//Disable this handler
this.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
System.out.print("[Different Handling]\t");
System.out.print(thread.getName());
System.out.print("\n");
throwable.printStackTrace(System.out);
System.out.println("[End Different Handler]");
}
});
}
public void run()
{
throw new RuntimeException("This is thrown in the child thread.");
}
}.start();
try
{
Thread.sleep(500);
}
catch (InterruptedException e1)
{
e1.printStackTrace();
}
JFrame gui = new JFrame();
gui.setSize(400, 300);
gui.setTitle("Swing Thread");
JButton error = new JButton("Throw Error");
error.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
throw new RuntimeException("This exception happens in the Swing thread");
}
});
gui.add(error);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
throw new RuntimeException("This is thrown in the main thread.");
}
}
This will give the following output (or something similar if you click the button):
[Different Handling] Thread-0
java.lang.RuntimeException: This is thrown in the child thread.
at Tests$2.run(Tests.java:45)
[End Different Handler]
[Handler Handling] main
java.lang.RuntimeException: This is thrown in the main thread.
at Tests.main(Tests.java:77)
[End Handling]
[Handler Handling] AWT-EventQueue-0
java.lang.RuntimeException: This exception happens in the Swing thread
at Tests$3.actionPerformed(Tests.java:68)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
<...snipped for brevity...>
[End Handling]
Thus, it is impossible to fully answer your question without additional information. If you are using JavaFX, for example, there is a different pattern for handling thrown exceptions. It depends entirely on your structure and situation. If you essentially set up the handler on the main thread and start there, then this should work without issue.
source to share