I gave a similar answer in another post, so yes, this is a copy of the past:
Something I do is to have a static method that handles all exceptions, and I add a log to JOptionPane to show it to the user, but you can write the result to a file in FileWriter that was corrupted in BufeeredWriter . For the main static method, to catch Uncout exceptions, I do:
SwingUtilities.invokeLater( new Runnable() { @Override public void run() {
As for the method:
public static void handleExceptions( Throwable ex, boolean shutDown ) { JOptionPane.showMessageDialog( null, "A CRITICAL ERROR APPENED!\n", "SYSTEM FAIL", JOptionPane.ERROR_MESSAGE ); StringBuilder sb = new StringBuilder(ex.toString()); for (StackTraceElement ste : ex.getStackTrace()) { sb.append("\n\tat ").append(ste); } while( (ex = ex.getCause()) != null ) { sb.append("\n"); for (StackTraceElement ste : ex.getStackTrace()) { sb.append("\n\tat ").append(ste); } } String trace = sb.toString(); JOptionPane.showMessageDialog( null, "PLEASE SEND ME THIS ERROR SO THAT I CAN FIX IT. \n\n" + trace, "SYSTEM FAIL", JOptionPane.ERROR_MESSAGE); if( shutDown ) { Runtime.getRuntime().exit( 0 ); } }
In your case, instead of βscreamingβ to the user, you can write a log, as I told you before:
String trace = sb.toString(); File file = new File("mylog.txt"); FileWriter myFileWriter = null; BufferedWriter myBufferedWriter = null; try {
Hope I helped.
To have a good day.:)
source share